Python 的机械化代理支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1997894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python's mechanize proxy support
提问by paul
I have a question about python mechanize's proxy support. I'm making some web client script, and I would like to insert proxy support function into my script.
我有一个关于 python mechanize 代理支持的问题。我正在制作一些 web 客户端脚本,我想在我的脚本中插入代理支持功能。
For example, if I have:
例如,如果我有:
params = urllib.urlencode({'id':id, 'passwd':pw})
rq = mechanize.Request('http://www.example.com', params)
rs = mechanize.urlopen(rq)
How can I add proxy support into my mechanize script?
Whenever I open this www.example.com
website, i would like it to go through the proxy.
如何将代理支持添加到我的机械化脚本中?每当我打开这个www.example.com
网站时,我都希望它通过代理。
回答by fulmicoton
I'm not sure whether that help or not but you can set proxy settings on mechanizeproxy browser.
我不确定这是否有帮助,但您可以在机械化代理浏览器上设置代理设置。
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:[email protected]: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")
回答by fulmicoton
You use mechanize.Request.set_proxy(host, type) (at least as of 0.1.11)
您使用 mechanize.Request.set_proxy(host, type) (至少从 0.1.11 开始)
assuming an http proxy running at localhost:8888
假设在 localhost:8888 上运行一个 http 代理
req = mechanize.Request("http://www.google.com")
req.set_proxy("localhost:8888","http")
mechanize.urlopen(req)
Should work.
应该管用。