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()
.