Python Scrapy can';t从类中提取文本
请查看以下html代码:Python Scrapy can';t从类中提取文本,python,css,python-2.7,css-selectors,scrapy,Python,Css,Python 2.7,Css Selectors,Scrapy,请查看以下html代码: <header class="online"> <img src="http://static.flv.com/themes/h5/img/iconos/online.png"> <span>online</span> <img src="http://static.flv.com/themes/h5/img/iconos/ojo16.png"
<header class="online">
<img src="http://static.flv.com/themes/h5/img/iconos/online.png"> <span>online</span>
<img src="http://static.flv.com/themes/h5/img/iconos/ojo16.png"> 428 <p>xxfantasia</p>
</header>
我想我已经使用了正确的css选择器,但结果是空的
有什么帮助吗?CSS选择器
但是Scrapy使用::text
伪元素扩展了CSS选择器,因此您希望使用cam.CSS('::text').extract()
,它应该提供与cam.xpath('.//text()').extract()相同的内容
注意:Scrapy还添加了::attr(attribute_name)
函数伪元素,以使用cam.CSS('header.online::text').extract()提取属性值(这在标准CSS选择器中也是不可能的)
如何只获取[u'428']
?您可以使用Python的strip()
和filter()
方法,例如:filter(bool,[e.strip()表示cam.css中的e('header.online::text')。extract()])
非常有效。谢谢
def parse(self, response):
sel = Selector(response)
cams = sel.css('header.online')
for cam in cams:
print cam.css('text').extract()