Overview
XPath
在浏览器中使用 XPath
F12 -> Console:
> $x("<xpath>")
例子:
<div class="grade-box clearfix">
<dl>...略...</dl>
<dl>
<dd title="60852">
6万+ </dd>
</dl>
<dl>...略...</dl>
</div>
- 取出元素的具体值(比如,取出“6万+” )
> $x('//div[@class="grade-box clearfix"]//dl[2]//dd')[0]["innerHTML"]
" 6万+ "
- 取出元素的具体文本(比如,取出“6万+” 但是不带空白符)
> $x('//div[@class="grade-box clearfix"]//dl[2]//dd')[0]["innerText"] "6万+"
- 取出元素的 attribute
> $x('//div[@class="grade-box clearfix"]//dl[2]//dd/@title')[0]["textContent"] "60852" > $x('//div[@class="grade-box clearfix"]//dl[2]//dd/@title')[0]["value"] "60852"
总结:
- xpath 中间如果有多个匹配,使用
[1]
或[2]
或[3]
… 这样的索引选择(从 1 开始!)。 - 获取 attribute,在元素基础 xpath 上增加
/@
表示后面跟的是 attribute,写全即//*//<xpath>/@<attribute>
。 $x()
执行获得的有效结果总是 array。- 对
$x()
执行得到的 array 结果,如果 xpath 精确的化,一般即是我们想要的在[0]
位置,且只有这一个。 $x()[0]
中的结果相当于字典,通过$x()[0]["key"]
取值。
在 scrapy 的 response
中使用 xpath - n/a
n/a
常见问题
AttributeError: ‘str’ object has no attribute ‘iter’
在使用 rules
+ Rule(LinkExtractor(...), ...)
时遇到这个问题。
原因一:
LinkExtractor
中的 restrict_xpaths
期望的是指到“元素”的 xpath,也就说不能在其里面有 ***/@<attr>
这样的写法。
如果想要取得的是 attribute,则定义(放到) LinkExtractor
内的 attrs
参数中去。
部署在 Scrapyd 上
$ pip install scrapyd
$ pip install scrapyd-client
$ pwd
<path>/<to>/<project>
$ vim ./scrapy.cfg
#### uncomment url
[deploy]
url = http://localhost:6800/
project = posts
问题
builtins.AttributeError: ‘int’ object has no attribute ‘splitlines’
参考:https://blog.csdn.net/qq_29719097/article/details/89431234
安装 scrapyd(scrapyd-client)支持的版本
- Scrapy==1.6.0
- Twisted==18.9.0
$ pip uninstall twisted
$ pip uninstall scrapy
$ pip install twisted==18.9.0
$ pip install scrapy==1.6.0
scrapydweb – 增强 Scrapyd
n/a
单元测试 - TODO
n/a
分布式 - TODO
N/A
更多功能
Scrapy 缓存
在开发环境爬虫中断了也是不好的感觉,要重启 scrapy 但是原先爬过的内容没有做处理的情况下就会再爬取一次。
通过缓存可以在重启的时候绕过已爬过的页面。
使用实例 – 待续
User-Agent
-
学习与操作:视频课程 - 反爬虫 - 设置随机请求头
-
GitHub 上做好的“插件”库:random-useragent
代理
-
GitHub 上的代理“插件”库: scrapy-proxies
- 写 middleware 类(安装了 scrapy 的“插件”库可以不用写,因为库里面已经是 middleware 类了)。
- 修改 settings.py 启用该 middleware 类即可。
Issue
-
运行时出现如下问题:
twisted.web.error.SchemeNotSupported: Unsupported scheme: b''
Solution:
可能原因是request.meta['proxy'] = proxy
这其中的proxy
value 没有写全。比如需要写成:
proxy = "http://3.2.1.1:8080"
(可能漏了 “http://” 没有写上)。check here for detail.
Scrapy 的设置 – TODO
N/A