python - 如何使用Python 3.4通过代理访问HTTPS网页

标签 python https proxy

我正在尝试访问具有登录名的 HTTPS 网页。无论我做什么都无法访问它。这是我在下面尝试的代码。

import urllib.request
from bs4 import BeautifulSoup

proxy = urllib.request.ProxyHandler({'http':'http://proxyName:proxyNumber'})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
response = urllib.request.urlopen('https://salesforce.com')
datum = response.read()
#.decode("UTF-8")
#response.close()
print(datum)

这是错误

File S:\...py" line 8 in module
reponse = urllib.request.urlopen("https://salesforce.com)
urllib.error.URLError:(urlopen error [WinError 10061] No connection could be made because the target machine actively refused it)

请帮忙

这是另一次尝试,出现新错误。我感觉我已经很接近了!

import urllib.request
proxies = {'https': 'http://proxyName:ProxyNumber'}
opener = urllib.request.build_opener(proxies)

#urllib.request.get("https://login.salesforce.com/", proxies=proxies)
urllib.request.install_opener(opener)
response = urllib.request.urlopen("https://login.salesforce.com/", proxies=proxies)

这是错误消息:

File S:/...py, line 6 in (module)
urllib.request.build_opener(proxies)
TypeError:expected BaseHandler instance, got (class 'dict)

最佳答案

如果您可以使用第三方模块,这里有一个使用请求模块的简单解决方案。

import requests

proxies = {
  "http": "http://proxyName:proxyNumber"
}

requests.get("https://salesforce.com", proxies=proxies, auth=('user', 'pass'))

改编自http://docs.python-requests.org/en/latest/user/advanced/#proxies

关于python - 如何使用Python 3.4通过代理访问HTTPS网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29524307/

相关文章:

python - Python-Python是否支持指针?

python - 使用 for 迭代 python 列表时如何指定类型/类名

python - Django:无法运行 django-admin.py

java - 如何禁用tomcat的https

php - 什么是 Chrome 数据压缩代理?

python - 如何在 python 中使用套接字响应代理服务器中的 "CONNECT"方法请求?

python - numpy.delete 将 float 组更改为字符串

https - 让 HTTPS 与 Traefik 和 GCE Ingress 配合使用

java - 与代理的 HTTPS 连接导致 SSLHandshakeException

node.js - AWS API 网关 : NodeJs post request - "Authorization not configured" on cognito logged in user