简体   繁体   English

如何在Scrapy中使用全局变量解析响应?

[英]How do I parse a response using a global variable in Scrapy?

I modified my start_requets method to be: 我将start_requets方法修改为:

def start_requests(self):
    array = list(open("file", 'r'))
    for i in array:
        yield self.make_requests_from_url("http://example.org/test.php?id=" + i)

How can I access the value of i in the parse method? 如何在解析方法中访问i的值?

I tried setting a global variable idd and adding 我尝试设置全局变量idd并添加

global idd
idd = i

in the start_requests method and then start_requests方法中,然后

def parse(self, response):
    item = DataItem()
    item['id'] = idd

But all of items' id fields were filled with idd's last value. 但是所有项目的id字段都填充了idd的最后一个值。

How do I fix this? 我该如何解决?

One (and probably the easiest) option would be to pass it inside meta : 一种(可能是最简单的)选择是将其传递给meta

yield scrapy.Request("http://example.org/test.php?id=" + i, 
                     meta={"index": i},
                     dont_filter=True)

Then, read it in parse() : 然后,在parse()读取它:

def parse(self, response):
    index = response.meta["index"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM