Selector的用法
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