我尝试使用parse作为一个中心集线器来收集来自其他解析函数的dict,因为我需要使用多个URL和域,但是我似乎不知道如何将所有数据都放回一个字典中。下面是我要做的一个例子:def parse(self, response):
request_1 = scrapy.Request(domain_1_url, callback = parse_2)
request_2 = scrapy.Request(domain_2_url, callback = parse_3)
#unpacks the two dicts from the 2 requests and return them as 1 to the pipeline
yield {**request_1, **request_2} #what I want to do, doesn't work because they are requests, not dicts
def parse_2(self, response):
yield {'someKey': 'some value'}
def parse_3(self, response):
yield {'someOtherKey': 'some more values'}
有什么办法我能做到吗?或者,有没有更好的方法可以做到这一点,比如把所有的东西都扔到管道中,然后让这个句柄来组合数据?在
编辑:
我考虑过将一个Item对象传递给其他请求,因为Python的“call by object”属性,在其他请求中修改它也会修改主解析中的Item对象:
^{pr2}$
不太清楚为什么这不起作用,但是parse()中的item对象在调用2个请求后仍然是空的