使用Scrapy爬虫获取代理IP地址

5

我使用 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地址?
3个回答

16

您可以放弃第一次请求以检查您的公共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    

添加另一个用于检查 IP 地址的 URL。这不需要解析响应对象,因为正文只包含 IP 地址。def start_requests(): yield Request('http://icanhazip.com/', callback=self.check_ip) # 如果需要,可以在此处从 start_urls 中生成其他请求def check_ip(self, response): print "我的公网 IP 是:" + response.body - cyn0
@bosnjak,我按照你的代码操作了,但是爬虫从未运行。原因是你的函数名存在拼写问题。 start_reqests -> start_requests - jis0324

8

最快的选项是使用scrapy shell并检查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'}

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


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接