class MyUserAgentMiddleware(UserAgentMiddleware): def __init__(self, user_agent): self.user_agent = user_agent @classmethod def from_crawler(cls, crawler): o = cls(Faker()) crawler.signals.connect(o.spider_opened, signal=signals.spider_opened) return o def process_request(self,request,spider): request.headers.setdefault('User-Agent',self.user_agent.user_agent())
配置
DOWNLOADER_MIDDLEWARES = {
#'middle.middlewares.MyCustomDownloaderMiddleware': 543,
'middle.middlewares.MyUserAgentMiddleware': 100,
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
}
注意 @classmethod不能删除
#!/usr/bin/python # -*- coding: UTF-8 -*- class A(object): bar = 1 def func1(self): print ('foo') @classmethod def func2(cls): print ('func2') print (cls.bar) cls().func1() # 调用 foo 方法 A.func2() # 不需要实例化