class RandomUserAgentMiddleware(object): def __init__(self, crawler): super(RandomUserAgentMiddleware, self).__init__() self.ua = UserAgent() self.per_proxy = crawler.settings.get('RANDOM_UA_PER_PROXY', False) self.ua_type = crawler.settings.get('RANDOM_UA_TYPE', 'random') self.proxy2ua = {} @classmethod def from_crawler(cls, crawler): return cls(crawler) def process_request(self, request, spider): def get_ua(): '''Gets random UA based on the type setting (random, firefox…)''' return getattr(self.ua, self.ua_type) if self.per_proxy: proxy = request.meta.get('proxy') if proxy not in self.proxy2ua: self.proxy2ua[proxy] = get_ua() logger.debug('Assign User-Agent %s to Proxy %s' % (self.proxy2ua[proxy], proxy)) request.headers.setdefault('User-Agent', self.proxy2ua[proxy]) else: ua = get_ua() request.headers.setdefault('User-Agent', get_ua())
@classmethod
def from_crawler(cls, crawler):
return cls(crawler)
我知道@classmethod是个类方法
cls后面打点可以调用类属性
但这里cls后面加 ( ) 是个什么鬼啊?
这里return出去的又是个什么东西啊?
他要return给谁啊?
@classmethod
def from_crawler(cls, crawler):
return cls(crawler)
他究竟想表达什么意思啊?
芒果DB0
相关分类