EthanZhang
EthanZhang

Reputation: 23

xpath how to extract the href attribute value based on the content?

<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>

I'm working on the scrapy how can I grap the href attribute value based on the content "next" in xpath?

Upvotes: 1

Views: 1867

Answers (1)

alecxe
alecxe

Reputation: 473893

Check the b element's text inside this way:

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

Demo from the 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'

Upvotes: 1

Related Questions