xpath如何根据内容提取href属性值?

时间:2015-12-28 05:41:05

标签: xpath scrapy

<a class="tab-link"> hoho </a>
<a class="tab-link" href="screener.ashx?v=110&amp;s=ta_topgainers&amp;r=41"><b>next</b></a>
<a class="tab-link"> hohaao </a>

我正在研究scrapy 如何根据内容&#34; next&#34;来绘制href属性值?在xpath?

1 个答案:

答案 0 :(得分:0)

以这种方式检查b元素的文字:

//a[b = "next"]/@href

来自Scrapy Shell的演示:

$ cat index.html
<div>
    <a class="tab-link"> hoho </a>
    <a class="tab-link" href="screener.ashx?v=110&amp;s=ta_topgainers&amp;r=41"><b>next</b></a>
    <a class="tab-link"> hohaao </a>
</div>
$ scrapy shell file:////path/to/index.html 
>>> response.xpath('//a[b = "next"]/@href').extract_first()
u'screener.ashx?v=110&s=ta_topgainers&r=41'