9

我对 python mechanize 的代理支持有疑问。我正在制作一些网络客户端脚本,我想在我的脚本中插入代理支持功能。

例如,如果我有:

params = urllib.urlencode({'id':id, 'passwd':pw})
rq = mechanize.Request('http://www.example.com', params) 
rs = mechanize.urlopen(rq)

如何将代理支持添加到我的机械化脚本中?每当我打开这个www.example.com网站时,我都希望它通过代理。

4

2 回答 2

31

我不确定这是否有帮助,但您可以在机械化代理浏览器上设置代理设置。

br = 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)
br.add_proxy_password("joe", "password")
于 2010-01-04T07:11:59.643 回答
9

您使用 mechanize.Request.set_proxy(host, type) (至少从 0.1.11 开始)

假设一个 http 代理在 localhost:8888 运行

req = mechanize.Request("http://www.google.com")
req.set_proxy("localhost:8888","http")
mechanize.urlopen(req)

应该管用。

于 2010-03-25T20:14:16.850 回答