帮助解决奇怪的 Python 抓取错误。一台机器在其他机器上运行时出现 HTTPError
我正在使用代理,以下是代码。
20 req = urllib2.Request(url)
21 # run the request for each proxy
22 # now set the proxy
23 req.set_proxy(proxy, "http")
24 req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
25 req.add_header('Referer', 'http://www.google.com/')
26 # time it
27 start = time.time()
28 # try to open the URL
29
30 time.sleep(maxwait*random.random())
31 #response = urllib2.urlopen(req)
32 opener = urllib2.build_opener()
33 response = opener.open(req).read()
在一台机器上工作正常(直接使用 eclipse 和 Firefox),但在另一台机器(不同 IP)的终端上运行时失败,并出现以下错误。
urllib2.HTTPError: HTTP Error 403: Forbidden
有线索吗?谢谢你们!
I am using a proxy and following is the code.
20 req = urllib2.Request(url)
21 # run the request for each proxy
22 # now set the proxy
23 req.set_proxy(proxy, "http")
24 req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
25 req.add_header('Referer', 'http://www.google.com/')
26 # time it
27 start = time.time()
28 # try to open the URL
29
30 time.sleep(maxwait*random.random())
31 #response = urllib2.urlopen(req)
32 opener = urllib2.build_opener()
33 response = opener.open(req).read()
Works fine on one machine (with eclipse and Firefox directly) but fails while running it at the terminal on another machine (different IP) with the following error.
urllib2.HTTPError: HTTP Error 403: Forbidden
Any leads? Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该脚本可以在第一台机器上运行吗?
检查此处以查看代理是否正常工作:http://www.domaintools.com/research/my -ip/
Does that script work on the first machine?
Check here to see if proxy is working: http://www.domaintools.com/research/my-ip/