我正在使用http代理和Mechanize模块.我初始化机械对象并按如下所示设置代理:
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("google")我的问题是机械化似乎完全忽略了代理.如果我调试并检查br对象,则在代理处理程序下可以看到我的代理设置.但是,即使我给出了不好的代理,Mechanize也像我从未设置代理一样进行业务.有什么作用?
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?
我也尝试过:
mechanize.install_opener(mechanize.build_opener(mechanize.ProxyHandler({'http': "127.0.0.1:99"}))) response = mechanize.urlopen("google")似乎也忽略了我的代理. (我什至没有给它提供有效的代理,它是否不会因为URLError而失败?)
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?)
推荐答案在电子邮件列表上进行讨论后将其弄清楚:
Figured it out after talking on the email list:
import mechanize browser = mechanize.Browser() browser.set_proxies(proxies={"http": "myproxy.example:1234"}, proxy_bypass=lambda hostname: False)更多推荐
代理似乎被Mechanize忽略了?
发布评论