Skip to content
Advertisement

Can not access array element in Python comming from Xpath

I am trying to cycle through an array in python:

# pull all shops into array
selectors = response.xpath('//div[@class="shop"]')
# cycle through all elements
for selector in selectors:

Somehow this does not work as it always is accessing the first element. Looking into the xpaths manually I do get the following:

>>> selectors = response.xpath('//div[@class="shop"]')
>>> selectors[7].xpath('//a[@class="name"]/@href').extract_first()
'/redirect/id/22216/ppn/1100410330'

# direct access
>>> response.xpath('//div[@class="shop"][8]//a[@class="name"]/@href').extract_first()
'/redirect/id/31/ppn/1100410330'

Accessing the element directly (note 8=7 due to 0), I am able to get it. Just cycling through it does not work out.

It cycles correclty n amount of times, but I am always getting the first element.

Advertisement

Answer

I guess you want selector.xpath('.//a[@class="name"]/@href').extract_first().

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement