我在<BODY>
中有一段HTML代码,我试图用scrapy选择:
<section class="content">
<div class="social clearfix">
<div class="profile profile-nano pull-left">
<a href="https://xxx" title="xxx"> <img src="/xxx" class="avatar" height="48" width="48" title="xxx" alt="xxx"> </a>
</div>
<p class="byline pull-left text-left"><strong>BY <a class="text-uppercase" href="https://xxx">xxx</a><br />
September 07, 2015</strong> </p>
这是我用来获取日期的xpath选择器:
response.selector.xpath('//p/@byline/text()')
返回null结果。
我的xpath选择器出错了什么?
答案 0 :(得分:2)
//p/@byline/text()
无法匹配任何内容,因为您基本上是尝试从byline
元素获取p
属性,并且提供的p
元素没有{{1属性。
您可以在具有byline
类的a
元素中获得div
元素的以下兄弟:
byline
或者,您可以从相应的In [1]: response.xpath("//p[contains(@class, 'byline')]//a/following-sibling::text()").extract()[0].strip()
Out[1]: u'September 07, 2015'
元素中获取所有文本节点,并通过re:test()
function使用正则表达式模式检查所需的文本节点:
p