当需要HTTPS和代理身份验证时,Python机械化不起作用 [英] Python mechanize doesn't work when HTTPS and Proxy Authentication required

查看:126
本文介绍了当需要HTTPS和代理身份验证时,Python机械化不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 2.7.2和Mechanize 0.2.5。

当我访问Internet时,我必须通过代理服务器。我写了下面的代码,但在最后一行发生了URLError ..有没有人对此有任何解决方案?

I use Python 2.7.2 and Mechanize 0.2.5.
When I access the Internet, I have to go through a proxy server. I wrote the following codes, but an URLError occurred at the last line.. Does anyone have any solution about this?

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


推荐答案

我建议你不要使用Mechanize,它已经过时了。看看请求它将使你的生活变得更轻松。使用具有请求的代理就是这样:

I don't recommend you to use Mechanize, it's outdated. Take a look at requests it will make your life a lot easier. Using proxies with requests it's just this:

import requests

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

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

这篇关于当需要HTTPS和代理身份验证时,Python机械化不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆