Skip to main content
 首页 » 编程设计

python之代理似乎被 Mechanize 忽略了

2025年05月04日9zhenyulu

我正在使用 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 而失败吗?)

请您参考如下方法:

在讨论电子邮件列表后想通了:

import mechanize 
browser = mechanize.Browser() 
browser.set_proxies(proxies={"http": "myproxy.example.com:1234"}, 
                proxy_bypass=lambda hostname: False)