smviswa
smviswa

Reputation: 111

Scrapy Xpath with text() equal to

    import scrapy
    example='<div class="ParamText"><span>OWNER APP</span></div>
<div class="ParamText"><span>OWNER</span></div>
<div class="ParamText"><span>OWNER NAME</span></div>'
    scrapy.Selector(text=example).xpath('//*[@class="ParamText"]/span[contains(text(),"OWNER")]').extract_first()

Here I need to scrape OWNER only sometimes 3 span I will get OWNER

output:

I am getting: OWNER APP

I want: OWNER

Upvotes: 1

Views: 886

Answers (3)

Gallaecio
Gallaecio

Reputation: 3857

You can use the regular expression ^OWNER$ to match spans containing only OWNER.

Replace contains(text(),"OWNER") with re:test(text(),"^OWNER$").

The advantage of regular expressions is that you could also allow for spaces (^\s*OWNER\s*$) or support different letter cases ((?i)^OWNER$).

Upvotes: 1

vezunchik
vezunchik

Reputation: 3717

You can select by text equation like scrapy.Selector(text=txt).xpath('//*[@class="ParamText"]/span[text()="OWNER"]').get() or without span details, it will give you the first one: scrapy.Selector(text=txt).css('div.ParamText span').get()

Upvotes: 0

scrapy.Selector(text=example).xpath('//*[@class="ParamText"]/span/text()').extract()[1]

Upvotes: 0

Related Questions