【发布时间】:2014-09-14 18:22:59
【问题描述】:
这是我想要实现的目标:
class Hello(Spider):
#some stuff
def parse(self, response):
#get a list of url of cities using pickle and store in a list
#Now for each city url I have to get list of monuments (using selenium) which is achieved by the below loops
for c in cities:
#get the list of monuments using selenium and iterate through each monument url contained in the division
divs = sel.xpath('some xpath/div')
for div in divs:
monument_url=''.join(div.xpath('some xpath'))
#For each monument url get the response and scrape the information
yield Request(monument_url, self.parse_monument)
def parse_monument(self, response):
#scrape some information and return to the loop(i.e. return to "for div in divs:")
现在发生的事情是:
1.在yield语句执行之前,我得到了全市所有古迹的列表。
2. 每当执行 yield 语句时,它会转到 parse_monument 函数并且不会返回循环,只会抓取第一个城市中存在的纪念碑列表。
有没有办法做到这一点?有什么方法可以获取 request 方法传递给 parse_monument 的响应对象,而无需转到 parse_monument 方法,以便我可以使用选择器从响应中选择我需要的元素?
谢谢你!!
【问题讨论】:
-
在
yield和parse_monument之前添加一些print,看看它是如何工作的。 -
和我之前提到的一样。在 parse_monument 中产生第一个和第二个之前。