2

How can we traverse back to parent in xpath?

I am crawling IMDB, to obtain genre of films, I am using

elem = hxs.xpath('//*[@id="titleStoryLine"]/div/h4[text()="Genres:"]')

Now,the genres are listed as anchor links, which are siblings to this tag. how can this be achieved?

Amrith Krishna
  • 2,768
  • 3
  • 31
  • 65

2 Answers2

2

This will select the parent element of the XPath expression you gave:

//*[@id="titleStoryLine"]/div/h4[text()="Genres:"]/..

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
2

For lxml only (not the built-in ElementTree), the parent is in the element. You can get it like this:

parent = elem.getparent()

and then look from there for whatever you need.

Corley Brigman
  • 11,633
  • 5
  • 33
  • 40