简体   繁体   English

Scrapy:TypeError:__ init __()缺少1个必需的位置参数:'settings'

[英]Scrapy: TypeError: __init__() missing 1 required positional argument: 'settings'

I have a Scrapy middleware: 我有一个Scrapy中间件:

class ProxyMiddleware(object):
    def __init__(self, settings):
        self.proxy_file = settings.get('PROXY_FILE')
        fin = open(self.proxy_file)
        self.proxy_list = list()
        for line in fin.readlines():
            parts = line.strip().split()
            proxy = parts[2] + '://' + parts[0] + ':' + parts[1]
            self.proxy_list.append(proxy)

    def process_request(self, request, spider):
        request.meta['proxy'] = random.choice(self.proxy_list)

But an error occurred when running, what's the settings? 但运行时出错,设置是什么?

If you need a settings object to initialize your middleware, you need to define a from_crawler() class method, otherwise scrapy initializes the middleware without arguments . 如果需要settings对象来初始化中间件,则需要定义from_crawler()类方法, 否则scrapy会在不使用参数的情况下初始化中间件

Take a look at the built-in middleware for inspiration, for example HttpErrorMiddleware 看看内置的中间件获取灵感,例如HttpErrorMiddleware

class HttpErrorMiddleware(object):

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

    def __init__(self, settings):
        self.handle_httpstatus_all = settings.getbool('HTTPERROR_ALLOW_ALL')
        self.handle_httpstatus_list = settings.getlist('HTTPERROR_ALLOWED_CODES')

In your case it would be something like: 在你的情况下,它将是这样的:

class ProxyMiddleware(object):

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

    def __init__(self, settings):
        self.proxy_file = settings.get('PROXY_FILE')
        fin = open(self.proxy_file)
        self.proxy_list = list()
        for line in fin.readlines():
            parts = line.strip().split()
            proxy = parts[2] + '://' + parts[0] + ':' + parts[1]
            self.proxy_list.append(proxy)

    def process_request(self, request, spider):
        request.meta['proxy'] = random.choice(self.proxy_list)

暂无
暂无

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

相关问题 Scrapy:TypeError:__init __()缺少1个必需的位置参数:'url' - Scrapy: TypeError: __init__() missing 1 required positional argument: 'url' TypeError:__init __()缺少1个必需的位置参数:“ to” - TypeError: __init__() missing 1 required positional argument: 'to' TypeError:__init __()缺少1个必需的位置参数 - TypeError: __init__() missing 1 required positional argument TypeError: __init__() 缺少 1 个必需的位置参数,scrapy 将参数传递给管道 - TypeError: __init__() missing 1 required positional argument with scrapy passing params to pipeline TypeError:__init __()缺少1个必需的位置参数:分支,但提供了参数 - TypeError: __init__() missing 1 required positional argument: branches, but argument is supplied 类型错误:__init__() 缺少 1 个必需的位置参数:“单位” - TypeError: __init__() missing 1 required positional argument: 'units' Tweepy错误 - TypeError:__ init __()缺少1个必需的位置参数:'listener' - Tweepy error - TypeError: __init__() missing 1 required positional argument: 'listener' TypeError:__init__() 缺少 1 个必需的位置参数:'successor - TypeError: __init__() missing 1 required positional argument: 'successor 类型错误:__init__() 缺少 1 个必需的位置参数:“父” - TypeError: __init__() missing 1 required positional argument: 'parent' Kivy MDDatePicker - 类型错误:__init__() 缺少 1 个必需的位置参数:“回调” - Kivy MDDatePicker - TypeError: __init__() missing 1 required positional argument: 'callback'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM