代理似乎被 Mechanize 忽略了?
我正在使用 http 代理和 Mechanize 模块。我初始化 mechanize 对象并设置代理,如下所示:
self.br = mechanize.Browser()
self.br.set_proxies({"http": proxyAddress}) #proxy address is like 1.1.1.1:8080
然后我像这样打开站点:
response = self.br.open("http://google.com")
我的问题是 mechanize 似乎完全忽略了代理。如果我调试并检查 br 对象,在代理处理程序下我可以看到我的代理设置。然而,即使我提供了一个糟糕的代理,Mechanize 也会像我从未设置过代理一样继续其业务。什么给?
编辑: 我也尝试过:
mechanize.install_opener(mechanize.build_opener(mechanize.ProxyHandler({'http': "127.0.0.1:99"})))
response = mechanize.urlopen("http://google.com")
它似乎也忽略了我的代理。 (我什至没有给它一个有效的代理,它不应该因 URLError 而失败吗?)
I am using an http proxy and the Mechanize module. I initialize the mechanize object and set the proxy like so:
self.br = mechanize.Browser()
self.br.set_proxies({"http": proxyAddress}) #proxy address is like 1.1.1.1:8080
Then I open the site like so:
response = self.br.open("http://google.com")
My problem is that mechanize seems to be completely ignoring the proxy. If I debug and inspect the br object, under the proxy handler I can see my proxy settings. However, even if I give a bad proxy Mechanize just goes about its business like I never set a proxy. What gives?
edit:
I have also tried:
mechanize.install_opener(mechanize.build_opener(mechanize.ProxyHandler({'http': "127.0.0.1:99"})))
response = mechanize.urlopen("http://google.com")
And it seems to be ignoring my proxy as well. (I didn't even give it a valid proxy, shouldn't it fail on a URLError?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
如果您尝试访问 https 站点,请将代理设置为 https,如下所示
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Want debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/4.0 (Compatible; MSIE 8.0; Windows NT 5.2; Trident/6.0)')]
br.set_proxies({"https": "XXX.XX.246.56:33835"})
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在讨论电子邮件列表后弄清楚了:
Figured it out after talking on the email list: