1. 還是以虎嗅為例,他給我返回的是一個json格式的json串
2.那么我需要操作的就是把json串轉換成我們的字典格式再進行操作
str=json.loads(response.body)['data'] #這邊是拿到響應體數據,然后進行序列化成字典,拿到字典中key為data的的值.是一個字符串
3.自己導入選擇器
from scrapy.selector import Selector
4.使用Selector的xpath方法獲取內容
result = Selector(text=你從json提取出來的str).xpath('你的xpath表達式').extract()
5.使用效果
我把上一篇虎嗅的在parse中修改了來示范一下
#處理數據 def parse(self, response): str=json.loads(response.body)['data'] result = Selector(text=str).xpath('//div[@class="mod-b mod-art"]/div[3]/h2/a/text()').extract() print('result===',result) #這邊處理比較難以理解,要多看看
5.文檔
當輸入 response.selector 時, 您將獲取到一個可以用於查詢返回數據的selector(選擇器),
以及映射到 response.selector.xpath() 、 response.selector.css() 的
快捷方法(shortcut): response.xpath() 和 response.css() 。