Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将额外的参数/值与start_url一起传递,以便在CrawSpider中使用?_Python_Scrapy - Fatal编程技术网

Python 如何将额外的参数/值与start_url一起传递,以便在CrawSpider中使用?

Python 如何将额外的参数/值与start_url一起传递,以便在CrawSpider中使用?,python,scrapy,Python,Scrapy,所以我在看这个问题: 这解决了我的部分问题。我基本上有一个长数组,例如: (ID、国家/地区、url) 通过使用start_请求,我成功地传递了ID和要另存为项目的国家以及我正在解析的其他项目 mapping = [(001, USA, url1), etc. etc.] def start_requests(self): for url, ID, country in self.mapping: yield Request(url, callback=s

所以我在看这个问题:

这解决了我的部分问题。我基本上有一个长数组,例如:

(ID、国家/地区、url)

通过使用start_请求,我成功地传递了ID和要另存为项目的国家以及我正在解析的其他项目

mapping = [(001, USA, url1), etc. etc.]
def start_requests(self):
        for url, ID, country in self.mapping:
            yield Request(url, callback=self.parse_items, meta={'country': country, 'ID': ID})
但是,当我使用爬行蜘蛛时,我的代码不起作用。它跳过了规则,并且在第一页之外不会进一步解析

rules=(Rule(lxmlinkextractor(restrict_xpath='//a[@name=“&lid=pagination next”]”) ,callback=“parse_items”,follow=True), )

我的问题是,如何保留我的初始映射元组的附加ID,并遵循爬行蜘蛛规则在这些网站上爬行?它可以抓取第一页,没有问题,但无法抓取


我还应该说,如果没有start_请求,并且只使用start_URL,它就不会爬行,但当然,我一辈子都不知道如何在抓取时标记与这些URL相关的“国家”和“id”

你确定你的规则是正确的并且有匹配的链接要遵循吗?谢谢
def parse_start_url(self, response):
    return self.parse_items(response)

def parse_items(self, response):
    country = response.meta['country']
    id = response.meta['id']

    items = []
    item['country'] = country
    item['id'] = id
    item['item1'] = response.xpath(...).extract()
    item['item2'] = response.xpath(...).extract()

...
...