帮助解决奇怪的 Python 抓取错误。一台机器在其他机器上运行时出现 HTTPError

发布于 2024-09-10 12:54:12 字数 794 浏览 1 评论 0原文

我正在使用代理,以下是代码。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天邊彩虹 2024-09-17 12:54:12

该脚本可以在第一台机器上运行吗?

检查此处以查看代理是否正常工作: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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文