鍍金池/ 問(wèn)答/Python  HTML/ scrapy的一些問(wèn)題和疑惑

scrapy的一些問(wèn)題和疑惑

我的代理中間件:settings已設(shè)置為544,默認(rèn)添加為None

class IpProxyMiddleware(object):
    def __init__(self, ip=''):
        self.ip = ip

    def process_request(self, request, spider):
        self.ip = requests.get('http://localhost:5555/random').text
        logging.info('當(dāng)前使用代理IP為:' + self.ip)
        request.meta['proxy'] = 'http://' + self.ip
  • 疑惑:每次使用Request指定一個(gè)url和回調(diào)函數(shù),都會(huì)執(zhí)行process_request方法嗎?然后調(diào)用一次API獲取本地的代理Ip?doc是這樣說(shuō)的
當(dāng)每個(gè)request通過(guò)下載中間件時(shí),該方法被調(diào)用
  • 問(wèn)題:如何設(shè)置回調(diào)的解析函數(shù)中,當(dāng)解析非200返回碼,重新切換代理IP?使用如下代碼是否有問(wèn)題?我使用了,還是無(wú)效。不知從何檢查。
        if response.status != 200:
            logging.error('--------IP has be baned!Retry again~ --------')
            yield Request(url=response.url,meta={'change_proxy': True}, callback=self.followees_parse)
回答
編輯回答
莫小染

debug一下看看唄,打斷點(diǎn)一步步運(yùn)行

2018年8月10日 20:59