当需要HTTPS和代理身份验证时,Python的机械浏览器无法工作

3

我使用Python 2.7.2和Mechanize 0.2.5。
当我访问互联网时,必须通过代理服务器。我编写了以下代码,但在最后一行出现了URLError错误。有没有人对此有解决方案?

import mechanize

br = mechanize.Browser()
br.set_debug_http(True)
br.set_handle_robots(False)

br.set_proxies({
    "http"  : "192.168.20.130:8080",
    "https" : "192.168.20.130:8080",})
br.add_proxy_password("username", "password")

br.open("http://www.google.co.jp/")  # OK
br.open("https://www.google.co.jp/") # Proxy Authentication Required
1个回答

3

我不建议您使用Mechanize,它已经过时了。看一下requests,它会让您的生活变得轻松许多。在requests中使用代理只需这样:

import requests

proxies = {
  "http": "10.10.1.10:3128",
  "https": "10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)

非常感谢。我不了解requests模块。我正在尝试使用它。如何指定代理身份验证的用户名和密码? - yutaka2487
你需要使用代理URL,例如:username:mypassword@10.10.1.10:3128。 - scripts
3
谢谢。您的方法肯定可以通过基本身份验证。当代理服务器需要摘要认证时,用户名和密码不能嵌入在代理URL中。我尝试了requests.auth.HTTPProxyAuth,但代理返回了407错误。 - yutaka2487

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接