Selector的用法
Selector是基于lxml構建的支持XPath選擇器、CSS選擇器,以及正則表達式,功能全面,解析速度和準確度非常高
from scrapy import Selectorbody = '<html><head><title>HelloWorld</title></head><body></body></html>'
selector = Selector(text=body)
title = selector.xpath('//title/text()').extract_first()
print(title)
單獨將Selector拿出來使用? ? 運行結果如下:
?由于Selector主要是與Scrapy結合使用
如Scrapy的回調函數中的參數response直接調用xpath()或者css()方法來提取數據
這里用官方文檔中的一列做演示
Selectors — Scrapy 2.13.3 documentationhttps://doc.scrapy.org/en/latest/topics/selectors.html
在命令行使用這一行代碼進入scrapy shell模式
?scrapy shell https://doc.scrapy.org/en/latest/topics/selectors.html
?