Usman Rafiq
Usman Rafiq

Reputation: 580

Xpath for finding text inside of an anchor tag using class (Scrapy)

I am trying to extract the link of Anchor Tag using Xpath

URL

<a class="text size-1x-small font-accent color-brand all-caps"
   href="http://time.com/section/business" 
   data-reactid="199">
       Business
</a>

Code

item["category"] = str(
    response.xpath(
        '//a[@class="text size-1x-small font-accent color-brand all-caps"]/text()'
    ).extract()
    )

And the python function

def parseSave(self, response):
    item = NYtimesItem()
    item["category"] = response.xpath(
        '//a[@class="text size-1x-small font-accent color-brand all-caps"]/text()'
    ).extract()

    yield item

Please tell me what I am doing wrong The expected output would be the text of Anchor Tag. e.g Business

Upvotes: 1

Views: 929

Answers (1)

Thiago Curvelo
Thiago Curvelo

Reputation: 3740

/text() is meant to get the element's inner text. To extract the href attribute use /@href instead.

Here is a handy xpath cheatsheet

Upvotes: 1

Related Questions