scrapy命令指南

2019-04-18  本文已影响0人  白衬衫ZM

<h1>
Scrapy指南
</h1>

<h2>
全局命令
</h2>

​ scrapy startproject spiderName :创建Spider项目

​ scrapy runspider scrpay_cn.py:基于文件运行爬虫

​ scrapy crawl SpiderClassName:基于项目运行爬虫

在当前目录运行CMD输入scrapy shell urls链接 进入调试命令行

scrapy shell 命令

response.css("标签名")

response.css("标签名").extract[0]:提前标签下第一个元素

response.css("标签名").extract_first():同上

response.css('.标签名'):".标签名"是class选择器

response.css('#标签名'):"#标签名"是id选择器

href值提前方式

response.css("标签名::attr(属性名"):

​ response.css("a::attr(href)")提取分页

​ response.css("img::attr(src)")提取图片

标签文本提取

​ response.css("p::text").extract()

​ response.css("title::text").extract()

​ response.css(".center::text").extract()

​ .........

<h2>
Scrapy提取数据“Xpath”
</h2>

表达式 描述
nodename 选取此节点的所有子节点。
/ 从根节点选取。
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
. 选取当前节点。
.. 选取当前节点的父节点。
@ 选取属性。

response.xpath("@href")提取分页数据

response.xpath("//ol//@href"):提取ol标签下所有的href属性值

例如HTML标签出现多个可以使用:标签[@属性名='属性值']

​ response.xpath("//ol[@class='page-navigator']//@href"):class属性提取

​ response.xpath("//ol[@id='page-navigator']//@href"):id属性提取

<h2>
提取标签里面的内容,表达式://text()
</h2>

​ reponse.xpath("//title//text()").extract()

<h2>
提取HTML标签的所有文字内容提取:string()
</h2>

​ response.xpath("string(//div[@class='post-content'])").extract()

上一篇下一篇

猜你喜欢

热点阅读