3.3 (补充 ) xpath一些语法认识以及scrapy shell
学习xpath,主要通过对网址http://doc.scrapy.org/en/latest/_static/selectors-sample1.html操作进行学习
认识xpath
(内容有待补充)
xpath的操作认识与学习
这儿列出有用的路径表达式:
表达式 | 描述 |
---|---|
nodename | 选取此节点的所有子节点。 |
/ | 从根节点选取。 |
// | 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 |
. | 选取当前节点。 |
.. | 选取当前节点的父节点。 |
@ | 选取属性。 |
一些实例学习:http://www.w3school.com.cn/xpath/xpath_syntax.asp
使用scrapy shell 学习xpath
首先,我们选择的进行学习的网站是:http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
使用浏览器查看源代码,可以看到结构:
<html>
<head>
<base href='http://example.com/' />
<title>Example website</title>
</head>
<body>
<div id='images'>
<a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
<a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
<a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
<a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
<a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
</div>
</body>
</html>
使用cmd命令提示符,键入命令打开shell:
scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
将会得到类似下列的数据:
2016-02-05 20:24:33 [scrapy] INFO: Scrapy 1.0.4 started (bot: scrapybot)
2016-02-05 20:24:33 [scrapy] INFO: Optional features available: ssl, http11
2016-02-05 20:24:33 [scrapy] INFO: Overridden settings: {'LOGSTATS_INTERVAL': 0, 'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter'}
2016-02-05 20:24:35 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, CoreStats, SpiderState
2016-02-05 20:24:36 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, Defau
aderStats
2016-02-05 20:24:36 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddlewar
2016-02-05 20:24:36 [scrapy] INFO: Enabled item pipelines:
2016-02-05 20:24:36 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-02-05 20:24:36 [scrapy] INFO: Spider opened
2016-02-05 20:24:37 [scrapy] DEBUG: Crawled (200) <GET http://doc.scrapy.org/en/latest/_static/selectors-sample1.html> (referer: None)
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x0000000002598C88>
[s] item {}
[s] request <GET http://doc.scrapy.org/en/latest/_static/selectors-sample1.html>
[s] response <200 http://doc.scrapy.org/en/latest/_static/selectors-sample1.html>
[s] settings <scrapy.settings.Settings object at 0x00000000029ADA90>
[s] spider <DefaultSpider 'default' at 0x5aa61d0>
[s] Useful shortcuts:
[s] shelp() Shell help (print this help)
[s] fetch(req_or_url) Fetch request (or URL) and update local objects
[s] view(response) View response in a browser
加载shell后会有一个response
获取。可以通过键入查询:
response
可以看到:
<200 http://doc.scrapy.org/en/latest/_static/selectors-sample1.html>
1.基本操作--获取网址的标题
从结构可以看到 标题在<title>...</title>之间文本中,所以我们可以用:
response.xpath('//title/text()')
选择根节点的儿子节点中选择名称为 title的节点。得到响应:
[<Selector xpath='//title/text()' data=u'Example website'>]
要是只想得到数据可以在后面加上 .extract():
response.xpath('//title/text()').extract()
这样响应变成:
[u'Example website']
2.基本操作--获取URL和image的链接
①获取URL
response.xpath('//base/@href').extract()
②获取image的链接
response.xpath('//a[contains(@href, "image")]/@href').extract()
③获取image的文本
response.xpath('//a[contains(@href, "image")]/text()').extract()
参考文档: