如何使用 Scrapy 抓取返回 JSON 的 Web 请求?例如,JSON 看起来像这样:
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
我希望抓取特定项目(例如上面的 name
和 fax
)并保存到 csv。
最佳答案
这与使用 Scrapy 的 HtmlXPathSelector
进行 html 响应相同。唯一的区别是你应该使用 json
模块来解析响应:
class MySpider(BaseSpider):
...
def parse(self, response):
jsonresponse = json.loads(response.text)
item = MyItem()
item["firstName"] = jsonresponse["firstName"]
return item
希望对您有所帮助。
https://stackoverflow.com/questions/18171835/
相关文章:
python - 如何在 Django REST 框架中返回自定义 JSON
java - 非空属性引用 transient 值 - transient 实例必须在当前操作之前保
json - 在 Play Framework JsObject 中解析 Json 数组
python - 在 View 中强制应用程序/json MIME 类型(Flask)
javascript - IE10/11 Ajax XHR 错误 - SCRIPT7002 : XM
python - 如何在保留矩阵维度的同时序列化 numpy 数组?
json - JSONObject 的 Jackson 2 等价物是什么?
c# - 如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET