I am trying to cycle through an array in python:
JavaScript
x
5
1
# pull all shops into array
2
selectors = response.xpath('//div[@class="shop"]')
3
# cycle through all elements
4
for selector in selectors:
5
Somehow this does not work as it always is accessing the first element. Looking into the xpaths manually I do get the following:
JavaScript
1
8
1
>>> selectors = response.xpath('//div[@class="shop"]')
2
>>> selectors[7].xpath('//a[@class="name"]/@href').extract_first()
3
'/redirect/id/22216/ppn/1100410330'
4
5
# direct access
6
>>> response.xpath('//div[@class="shop"][8]//a[@class="name"]/@href').extract_first()
7
'/redirect/id/31/ppn/1100410330'
8
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()
.