How to select all href attribute in a html tag contain a common class. in Scrapy

422 Views Asked by At

I want to select all the href contains in tag ... here is my html code

<a href="/gp/product/0545935172 ...." class="aok-block aok-nowrap" title="Dog Man: Lord of the Fleas: From the Creator of Captain Underpants (Dog Man #5)">

I used response.css('a.aok-block::attr(href)').extract() but the result is: [ ]

2

There are 2 best solutions below

0
On

It is recommended that you use xpath expressions. For your example response.xpath("//a[class='aok-block aok-nowrap']").get_attribute('href')

0
On

Add to the answer johnnydoe

Will be:

    response.xpath('*//a/@href').extract_first()
    response.xpath('*//a/@class').extract_first()
    response.xpath('*//a/@title').extract_first()

If you want get only href then you must find upper tag...like this:

    <li>
    <a id="nav-questions" href="/questions">
    </li>

Will be:

    response.xpath('...some uniq selector.../li/a/@href').extract_first()