Python的机械化代理支持

pau*_*aul 9 python mechanize

我有一个关于python mechanize的代理支持的问题.我正在制作一些Web客户端脚本,我想在我的脚本中插入代理支持功能.

例如,如果我有:

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

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

ful*_*ton 30

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

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")
Run Code Online (Sandbox Code Playgroud)


小智 9

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

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

req = mechanize.Request("http://www.google.com")
req.set_proxy("localhost:8888","http")
mechanize.urlopen(req)
Run Code Online (Sandbox Code Playgroud)

应该管用.