hjschmid hjschmid - 1 year ago 67
Python Question

Scrapy SgmlLinkExtractor how to scrape li tags with changing id's

How can I get an element at this specific location:

Check picture

The XPath is:


I got this path from google chrome browser. I basically want to retreive the number at this specific location with the following statement:

zimmer = response.xpath('//*[@id="id316"]/span[2]').extract()

However I'm not getting anything but an empty string. I found out that the id value is different for each element in the list I'm interested in. Is there a way to write this expression such that it works for generic numbers?

Answer Source

Use the corresponding label and get the following sibling element containing the value:

//span[. = 'Zimmer']/following-sibling::span/text()

And, note the bonus to the readability of the locator.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download