本文實例講述了Python基于scrapy采集數據時使用代理服務器的方法。分享給大家供大家參考。具體如下:
# To authenticate the proxy, #you must set the Proxy-Authorization header. #You *cannot* use the form http://user:pass@proxy:port #in request.meta['proxy']import base64proxy_ip_port = "123.456.789.10:8888"proxy_user_pass = "awesome:dude"request = Request(url, callback=self.parse)# Set the location of the proxyrequest.meta['proxy'] = "http://%s" % proxy_ip_port# setup basic authentication for the proxyencoded_user_pass=base64.encodestring(proxy_user_pass)request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
希望本文所述對大家的python程序設計有所幫助。
新聞熱點
疑難解答