Python Mechanize set_proxies 和 Internet 选项代理设置

标签 python proxy mechanize

我不确定是什么问题。我有一个使用 Mechanize 打开网页的脚本。我想为此设置代理。

我可以

br.set_proxies({'http':'proxyip:port'})

当我尝试打开网页时,它会超时。

但是,如果我不使用上面的代理选项,而是在 Windows 的 Internet 选项中设置相同的代理,在连接中,使用代理,脚本将工作并通过代理打开网页。

是什么赋予了?
Mechanize 代理支持究竟是如何工作的?它是否基于 Internet 选项代理设置?
如果是这样,为什么 Mechanize 中的 set_proxies 选项?
是否可以为 Mechanize 设置一个单独的代理,并且与 Internet 选项之一(如果有)不同,或者即使在 IOpt 中没有设置代理也可以工作。

任何帮助表示赞赏。

最佳答案

除了您提到的步骤之外,您还必须使用您的凭据进行身份验证(如果需要)才能使用 http 代理。否则, Mechanize 知道使用代理,但不知道如何进行身份验证。
更多详情请见:mechanizeDoc

br = mechanize.Browser()
# Explicitly configure proxies (Browser will attempt to set good defaults).
# Note the userinfo ("joe:password@") and port number (":3128") are optional.
br.set_proxies({"http": "joe:password@myproxy.example.com:3128",
"ftp": "proxy.example.com",
                })
# Add HTTP Basic/Digest auth username and password for HTTP proxy access.
# (equivalent to using "joe:password@..." form above)
# not necessary if credentials are not required
br.add_proxy_password("joe", "password")

您还必须检查您的防火墙是否配置为通过代理的流量。在 Windows 下,您可能还会遇到可以从浏览器发送流量的情况,因为这是在防火墙中配置的(作为源程序)。

除此之外,您可以使用多个代理,因为 Mechanize 代理不依赖于其他配置。

关于Python Mechanize set_proxies 和 Internet 选项代理设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20825708/

相关文章:

python - PYSPARK:CX_ORACLE.InterfaceError:不是查询

Python websocket 连接并监听传入消息

python - Python/Mechanize 中的 Http 错误 405/500(使用 mechanize 自动登录一个网站)

Perl WWW::Mechanize 和经过身份验证的代理

python - 如何从单个列表返回 2 个单独的列表?

Python CLI 程序单元测试

windows - IIS 上的 Grafana : Origin Not Allowed

oauth - nginx 负载均衡器和 OAuth

python - Mechanize browser.submit() 返回原始表单而不是预期结果

python - wxPython:我应该如何在 Controller 中组织每个小部件数据?