4.7css选择器

请参阅https://www.runoob.com/cssref/css-selectors.html

获取属性值

::attr({属性名})
::attr(href)

获取标签值

::text

如何在scrapy中使用xpath?

scrapy抓取到http源码之后,将抓取结果保存到response中,通过respense.xpath("{xpath 表达式}")即可过滤出想要的内容。

respense.xpath()返回一个scrapy.selector.unified.SelectorList对象

通过SelectorList的extract()可以返回一个unicode字符串列表

当中文字符以unicode编码方式显示时,请检查python版本,python2存在中文字符以unicode编码方式显示,python3不存在

命令行方式:scrapy shell {待抓取的url},示例如下

scrapy shell http://blog.jobbole.com

response.css(".entry-header h1")
response.css(".entry-header h1::text").extract()
response.css(".entry-header h1::text").extract()[0]

pycharm调试方式

debug模式下使用

Last updated

Was this helpful?