使用获取代理ip地址

2024-04-26 15:13:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Tor来抓取网页。 我启动了tor和polipo服务并添加

class ProxyMiddleware(object):   # overwrite process request   def
  process_request(self, request, spider):
     # Set the location of the proxy
    request.meta['proxy'] = "127.0.0.1:8123"

现在,我如何确保scrapy使用不同的IP地址来处理请求?


Tags: theself网页objectrequestdefprocessclass
2条回答

您可以发出第一个请求来检查您的公共IP,并将其与在不使用Tor/VPN的情况下转到http://checkip.dyndns.org/时看到的IP进行比较。如果它们不一样,scrapy显然使用了不同的IP。

def start_reqests():
    yield Request('http://checkip.dyndns.org/', callback=self.check_ip)
    # yield other requests from start_urls here if needed

def check_ip(self, response):
    pub_ip = response.xpath('//body/text()').re('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')[0]
    print "My public IP is: " + pub_ip

    # yield other requests here if needed    

最快的方法是使用^{}并检查meta是否包含proxy

从项目根目录开始:

$ scrapy shell http://google.com
>>> request.meta
{'handle_httpstatus_all': True, 'redirect_ttl': 20, 'download_timeout': 180, 'proxy': 'http://127.0.0.1:8123', 'download_latency': 0.4804518222808838, 'download_slot': 'google.com'}
>>> response.meta
{'download_timeout': 180, 'handle_httpstatus_all': True, 'redirect_ttl': 18, 'redirect_times': 2, 'redirect_urls': ['http://google.com', 'http://www.google.com/'], 'depth': 0, 'proxy': 'http://127.0.0.1:8123', 'download_latency': 1.5814828872680664, 'download_slot': 'google.com'}

这样您就可以检查中间件配置是否正确,请求是否通过代理。

相关问题 更多 >