Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 尝试访问request.meta';时出现KeyError错误;这是刮痧的钥匙_Python_Scrapy - Fatal编程技术网

Python 尝试访问request.meta';时出现KeyError错误;这是刮痧的钥匙

Python 尝试访问request.meta';时出现KeyError错误;这是刮痧的钥匙,python,scrapy,Python,Scrapy,下面是一个简单spider的代码: # -*- coding: utf-8 -*- import os from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors import LinkExtractor from scrapy.http import Request class AzS(CrawlSpider): name = 'azs' allowed_d

下面是一个简单spider的代码:

# -*- coding: utf-8 -*-

import os
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
from scrapy.http import Request

class AzS(CrawlSpider):

    name = 'azs'
    allowed_domains = ["******"]
    start_urls = [
        "*********" 
    ]

    rules = (
        Rule(LinkExtractor(restrict_xpaths = ("""//*[@id="yearList"]""")), callback = 'year', follow = True), # years at start url
        Rule(LinkExtractor(restrict_xpaths = ("""/html/body/div[3]/div/div[1]/div/div[2]/ul""")), callback = 'model', follow = True), # model
    )


    # start url with years list
    def year(self, response):
        yr = response.url.split('=')[-1][2:4]
        request = Request(response.url,
                          callback = self.model)
        request.meta['yr'] = yr
        return request


    # the page of the year with models list 
    def model(self, response):
        print response.meta['yr']
执行时,此代码会产生以下错误:

File "xxxxxxxxxx.py", line 33, in model
            print response.meta['yr']
        exceptions.KeyError: 'yr'

我无法找出导致此错误的原因,因此非常感谢您的帮助。提前谢谢。

因为您有两条规则,而且当第二条规则变为真时,显然会触发一些请求,并且从那里得到的响应是
模型
,在这种情况下,您不会使用
'yr'键设置任何
数据。这可能是错误的根本原因


您可以使用一些
try-except
或尝试使用
get()
访问密钥,即
response.meta.get('yr','your_value')
。如果未找到该
,它将以
您的_值
作为值

非常感谢您指出多个规则问题;try except和dict.get()不是解决方案,因为我需要在回调之间传递数据。有什么建议吗?哦,回答得好,吉钦。你是一个了不起的人:),+1但是
hasattr
是比
try except
更好的方法,如果你可以发布开始url,就很容易找到解决方案。