uzumaki_naruto
uzumaki_naruto

Reputation: 29

extract only one part from xpath scrapy

I want to extract from a web page information with xpath, but I'm getting the wrong information. In this code below I want to get 100 :

<div class="pricing">
 <p class="pricePerUnit">
  <p class="pricePerMeasure">
  £0.64
  <abbr title="per">/</abbr>
  100

I want to only get 100, I tried this but it returns £0.64 100. But, I just want to retrieve the 100 :

`prices_mesure3 = `response.xpath('//p[@class="pricePerMeasure"]/text()').extract()`

Any help please ?

Upvotes: 0

Views: 769

Answers (3)

Granitosaurus
Granitosaurus

Reputation: 21406

Xpath support node indexing, so you can just add [last()] or [2] to your xpath:

In: response.xpath('//p[@class="pricePerMeasure"]/text()[last()]').extract_first()
Out: u'\n  100 '

Upvotes: 1

Fran
Fran

Reputation: 81

Couldn't you just split the result and then take the last element?

prices_mesure3 = response.xpath('//p[@class="pricePerMeasure"]/text()').extract()[0].split()[-1]

Upvotes: 0

Andersson
Andersson

Reputation: 52665

You can try below XPath expression to get "100" only

//p[@class="pricePerMeasure"]/text()[last()]

P.S. I suppose there are only 2 text nodes ("£0.64" and "100") and you just missed closing tags...

Upvotes: 0

Related Questions