merlin
merlin

Reputation: 2917

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.

Upvotes: 0

Views: 34

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

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

Upvotes: 1

Related Questions