Python的机械化代理支持

时间:2010-01-04 06:51:05

标签: python mechanize

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

例如,如果我有:

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

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

2 个答案:

答案 0 :(得分:30)

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

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")

答案 1 :(得分:9)

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

假设在localhost运行的http代理:8888

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

应该工作。

相关问题