【发布时间】:2014-11-29 23:06:49
【问题描述】:
(Python 3.4.2) 任何人都可以帮助我使用 urllib 获取 https 页面吗?我花了几个小时试图弄清楚这一点。
这是我想要做的(非常基本的):
import urllib.request
url = "".join((baseurl, other_string, midurl, query))
response = urllib.request.urlopen(url)
html = response.read()
这是我运行时的错误输出:
File "./script.py", line 124, in <module>
response = urllib.request.urlopen(url)
File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 455, in open
response = self._open(req, data)
File "/usr/lib/python3.4/urllib/request.py", line 478, in _open
'unknown_open', req)
File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 1244, in unknown_open
raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: 'https>
我也尝试过使用 data=None 无济于事:
response = urllib.request.urlopen(url, data=None)
我也试过这个:
import urllib.request, ssl
https_sslv3_handler = urllib.request.HTTPSHandler(context=ssl.SSLContext(ssl.PROTOCOL_SSLv3))
opener = urllib.request.build_opener(https_sslv3_handler)
urllib.request.install_opener(opener)
resp = opener.open(url)
html = resp.read().decode('utf-8')
print(html)
此^ 脚本出现类似错误,在“resp = ...”行中发现错误并抱怨“https”是未知的 url 类型。
Python 是在我的计算机(Arch Linux)上使用 SSL 支持编译的。我试过几次重新安装python3和openssl,但这没有帮助。我没有尝试完全卸载python然后重新安装,因为我还需要卸载我电脑上的很多其他程序。
有人知道发生了什么吗?
-----编辑-----
感谢 Andrew Stevlov 的回答,我想通了。我的网址中有一个“:”,我猜 urllib 不喜欢这样。我用“%3A”替换了它,现在它可以工作了。非常感谢大家!!!
【问题讨论】:
标签: python-3.x https urllib