代理似乎被Mechanize忽略了? [英] Proxy seems to be ignored by Mechanize?

查看:109
本文介绍了代理似乎被Mechanize忽略了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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("http://google.com")

我的问题是机械化似乎完全忽略了代理.如果我调试并检查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("http://google.com")

似乎也忽略了我的代理. (我什至没有给它提供有效的代理,它是否不会因为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.com:1234"},
                proxy_bypass=lambda hostname: False)

这篇关于代理似乎被Mechanize忽略了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆