Scrapy 如何正确配置、验证xpath?

简介: [题记]:xpath难点在使用scrapy做爬取网页的时候,规则的匹配是关键。如:sel.xpath(‘//ul[@id=”dirlist”]/li/dl/dt’)的正确配置。如何进行xpath的正确配置呢? Scrapy shell交互终端是一个可供较快调试、验证配置结果的好工具。

1、Scrapy Shell介绍

Scrapy终端是一个交互终端,供您在未启动spider的情况下尝试及调试您的爬取代码。

其本意是用来测试提取数据的代码,不过您可以将其作为正常的Python终端,在上面测试任何的Python代码。

该终端是用来测试XPath或CSS表达式,查看他们的工作方式及从爬取的网页中提取的数据。 在编写您的spider时,该终端提供了交互性测试您的表达式代码的功能,免去了每次修改后运行spider的麻烦。

一旦熟悉了Scrapy终端后,您会发现其在开发和调试spider时发挥的巨大作用。


2、Scrapy Shell安装

如果您安装了 IPython ,Scrapy终端将使用 IPython (替代标准Python终端)。 IPython 终端与其他相比更为强大,提供智能的自动补全,高亮输出,及其他特性。 强烈推荐您安装 IPython ,特别是如果您使用Unix系统(IPython 在Unix下工作的很好)。

安装参考:

http://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/shell.html


IPython安装:

pip install ipython (前提python已安装)


3、启动终端 scrapy shell

使用命令格式:scrapy shell 待爬取的链接URL

举例如下:


[root@yang testing]# scrapy shell http://www.11467.com/tianjin/dir/a05-p4.htm

2016-10-13 14:10:27 [scrapy] INFO: Scrapy 1.1.2 started (bot: scrapybot)

2016-10-13 14:10:27 [scrapy] INFO: Overridden settings: {'LOGSTATS_INTERVAL': 0, 'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter'}

2016-10-13 14:10:27 [py.warnings] WARNING: :0: UserWarning: You do not have a working installation of the service_identity module: 'No module named pyasn1_modules.rfc2459'. P.


2016-10-13 14:10:27 [scrapy] INFO: Enabled extensions:

['scrapy.extensions.telnet.TelnetConsole',

'scrapy.extensions.corestats.CoreStats']

2016-10-13 14:10:27 [scrapy] INFO: Enabled downloader middlewares:

['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',

'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',

'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',

'scrapy.downloadermiddlewares.retry.RetryMiddleware',

'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',

'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',

'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',

'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',

'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',

'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',

'scrapy.downloadermiddlewares.stats.DownloaderStats']

2016-10-13 14:10:27 [scrapy] INFO: Enabled spider middlewares:

['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',

'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',

'scrapy.spidermiddlewares.referer.RefererMiddleware',

'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',

'scrapy.spidermiddlewares.depth.DepthMiddleware']

2016-10-13 14:10:27 [scrapy] INFO: Enabled item pipelines:

[]

2016-10-13 14:10:27 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6031

2016-10-13 14:10:27 [scrapy] INFO: Spider opened

2016-10-13 14:10:27 [scrapy] DEBUG: Crawled (200) <GET http://www.11467.com/tianjin/dir/a05-p4.htm> (referer: None)

2016-10-13 14:10:28 [traitlets] DEBUG: Using default logger

2016-10-13 14:10:28 [traitlets] DEBUG: Using default logger

[s] Available Scrapy objects:

[s] crawler <scrapy.crawler.Crawler object at 0x2e8d9d0>

[s] item {}

[s] request <GET http://www.11467.com/tianjin/dir/a05-p4.htm>

[s] response <200 http://www.11467.com/tianjin/dir/a05-p4.htm>

[s] settings <scrapy.settings.Settings object at 0x2e8d950>

[s] spider <DefaultSpider 'default' at 0x331ed90>

[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

In [1]:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

//验证xpath的正确性,若正确会有如下的输出;否则,则不会有输出。


In [1]: sel.xpath('//ul[@id="dirlist"]/li/dl/dt/h4/a/text()')

2016-10-13 14:12:46 [py.warnings] WARNING: shell:1: ScrapyDeprecationWarning: "sel" shortcut is deprecated. Use "response.xpath()", "response.css()" or "response.selector" instead


Out[1]:

[<Selector xpath='//ul[@id="dirlist"]/li/dl/dt/h4/a/text()' data=u'\u5929\u6d25\u5e02\u4e1c\u4e3d\u533a\u755c\u7267\u6c34\u4ea7\u5c40\u755c\u79bd\u826f\u79cd\u573a'>,

<Selector xpath='//ul[@id="dirlist"]/li/dl/dt/h4/a/text()' data=u'\u5929\u6d25\u5e02\u4e1c\u4e3d\u533a\u4e61\u9547\u755c\u7267\u517d\u533b\u7ba1\u7406\u7ad9'>,

<Selector xpath='//ul[@id="dirlist"]/li/dl/dt/h4/a/text()' data=u'\u5929\u6d25\u5e02\u4e1c\u4e3d\u533a\u8352\u8349\u5768\u4e61\u517d\u533b\u7ad9'>,

<Selector xpath='//ul[@id="dirlist"]/li/dl/dt/h4/a/text()' data=u'\u5929\u6d25\u5e02\u4e1c\u4e3d\u533a\u4e48\u516d\u6865\u56de\u65cf\u4e61\u519c\u4e1a\u516c\u53f8\u66d9\u5149\u79cd\u7f8a\u573a'>,

<Selector xpath='//ul[@id="dirlist"]/li/dl/dt/h4/a/text()' data=u'\u5929\u6d25\u5e02\u5927\u6e2f\u533a\u755c\u79bd\u826f\u79cd\u573a'>,

<Selector xpath='//ul[@id="dirlist"]/li/dl/dt/h4/a/text()' data=u'\u5929\u6d25\u5e02\u5927\u6e2f\u533a\u519c\u6797\u755c\u7267\u5c40\u755c\u7267\u517d\u533b\u7ad9\uff08\u5929\u6d25\u5e02\u5927\u6e2f\u533a\u52a8\u7269\u68c0\u75ab\u7ad9\uff09'>,

1

2

3

4

5

6

7

8

9

10

4.Xpath地址获取方式

sel.xpath(‘//ul[@id=”dirlist”]/li/dl/dt/h4/a/text()’)解析:

如下图所示:

//ul[@id=”dirlist”] :覆盖整个左侧区域;

/li/dl/dt/h4/a/text(): 定位到具体的公司名称。image.png

5.采集结果验证

采集目标:

"http://www.11467.com/tianjin/dir/a.htm",

"http://www.11467.com/tianjin/dir/a.htm",

.........

"http://www.11467.com/tianjin/dir/s.htm"

1

2

3

4

有效公司数目:16万8510条。

[root@yan scrapy_tianjin02]# cat tianjin1013.txt | wc -l

168510

[root@yan scrapy_tianjin02]# tail -f tianjin1013.txt

1

2

3

足千里(天津)科技有限公司

最美花开(天津)网络科技有限公司

醉潮尚(天津)餐饮管理有限公司

遵化市珍珠甘栗食品有限公司天津分公司

遵化燕山塔陵有限公司天津办事处

遵义神龙医药保健有限责任公司驻天津办事处

1

2

3

4

5

6

6.使用工具

1)浏览器:firefox

2) 插件:firepath, firebug

3)插件使用与下载地址:

http://blog.csdn.net/qiyueqinglian/article/details/49280221

相关文章
|
1月前
|
数据采集 Web App开发 搜索推荐
项目配置之道:优化Scrapy参数提升爬虫效率
项目配置之道:优化Scrapy参数提升爬虫效率
|
8月前
|
Ubuntu 搜索推荐 Python
Python | Scrapy必备之配置虚拟环境
Python | Scrapy必备之配置虚拟环境
|
4月前
|
数据采集 监控 中间件
解决HTTP 429错误的Scrapy中间件配置
解决HTTP 429错误的Scrapy中间件配置
|
11月前
|
Python
Python—scrapy框架配置及实用案例
Python—scrapy框架配置及实用案例
100 0
|
11月前
|
数据采集 NoSQL MongoDB
一日一技:通过参数修改Scrapy的配置信息
一日一技:通过参数修改Scrapy的配置信息
57 0
|
数据采集 XML 前端开发
Python爬虫:scrapy内置网页解析库parsel-通过css和xpath解析xml、html
Python爬虫:scrapy内置网页解析库parsel-通过css和xpath解析xml、html
147 0
|
Python
Scrapy添加代理验证
Scrapy添加代理验证
69 0
|
数据采集 JSON API
新手教程 | Python Scrapy框架HTTP代理的配置与调试
做过python爬虫的都知道,HTTP代理的设置时要在发送请求前设置好,那HTTP代理的逻辑点在哪里呢?实际上,只需要在Scrapy 的项目结构中添加就好
新手教程 | Python Scrapy框架HTTP代理的配置与调试
|
Python
Scrapy添加代理验证
Scrapy添加代理验证
110 0
|
数据采集 Python
配置Pycharm的Scrapy爬虫Spider子类通用模板
配置Pycharm的Scrapy爬虫Spider子类通用模板
190 0