我已经安装了Tor + Privoxy
在我的服务器上,它们工作正常! (已测试)。
但现在当我尝试使用urllib2 (python)
当然,使用代理来抓取谷歌购物结果,我总是被谷歌阻止(有时是503错误,有时是403错误)。那么任何人有任何解决方案可以帮助我避免这个问题吗?我们将非常感激!
我正在使用的源代码:
_HEADERS = {
'User-Agent': 'Mozilla/5.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'deflate',
'Connection': 'close',
'DNT': '1'
}
request = urllib2.Request("https://www.google.com/#q=iphone+5&tbm=shop", headers=self._HEADERS)
proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
try:
response = urllib2.urlopen(request)
html = response.read()
print html
except urllib2.HTTPError as e:
print e.code
print e.reason
请注意:当我不使用代理时,它可以正常工作!
你安装了Tor的控制器库stem吗?
只需几行代码,您就可以向 Tor 请求新的身份。看:
https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor
只需使用异常来捕获 403 和 503 错误,并通过请求新身份来处理它们,如上面的链接所示。祝你好运。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)