5

I am facing a problem where I have to get the result from the child node which may or may not be parents to some other node using Xpath in scrapy. consider the case like

<h1 class="main">
 <span class="child">data</span>
</h1>

or

<h1 class="main">
<span class="child">
 <span class="child2">data</span>
</span>
</h1>

My solution was response.xpath(".//h1[@class='main']/span/text()").extract()

2 Answers 2

3

use //text, and it will return all text elements in a list from within your span, both parent and child:

response.xpath(".//h1[@class='main']/span//text()").extract()
3
  • @PramodBisht, if my answer helps you solve the issue. Please accept my answer / upvote :)
    – Anzel
    Oct 30, 2014 at 5:06
  • 1
    to upvote atleast 15 points are required and I am beginner with only one points. I hope you understand.
    – Pramod
    Oct 30, 2014 at 9:26
  • @PramodBisht, appreciated :)
    – Anzel
    Oct 30, 2014 at 9:27
1

You can use:

  • response.xpath("string(.//h1[@class='main']/span)").extract()
  • or even response.xpath("string(.//h1[@class='main'])").extract() if you're after the whole header text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.