Scrapy 选择器


取自

http://scrapy-chs.readthedocs.org/zh_CN/latest/topics/selectors.html#removing-namespaces

contains():    #限制选择

response.xpath('//a[contains(@href, "image")]/img/@src').extract()
/a[contains(@href, "image")]:    #返回 在 a标签下, href元素中包含"image"的字符串
re:test():    #正则表达式限制
sel.xpath('//li[re:test(@class, "item-\d$")]//@href').extract()
/li[re:test(@class, "item-\d$")]:    #返回 li标签下 class元素中,匹配正则的字符串

移除命名空间

在处理爬虫项目时,完全去掉命名空间而仅仅处理元素名字,写更多简单/实用的XPath会方便很多。你可以为此使用 Selector.remove_namespaces() 方法。

让我们来看一个例子,以Github博客的atom订阅来解释这个情况。

首先,我们使用想爬取的url来打开shell:

$ scrapy shell https://github.com/blog.atom
一旦进入shell,我们可以尝试选择所有的 <link> 对象,可以看到没有结果(因为Atom XML命名空间混淆了这些节点):
>>> response.xpath("//link")
[]

但一旦我们调用 Selector.remove_namespaces() 方法,所有的节点都可以直接通过他们的名字来访问:

>>> response.selector.remove_namespaces()
>>> response.xpath("//link")
[<Selector xpath='//link' data=u'<link xmlns="http://www.w3.org/2005/Atom'>,
 <Selector xpath='//link' data=u'<link xmlns="http://www.w3.org/2005/Atom'>,
 ...


转载于:https://my.oschina.net/sii/blog/655853

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值