urllib.error.URLError: <urlopen error unknown url type: 'https> [英] urllib.error.URLError: <urlopen error unknown url type: 'https>

查看:118
本文介绍了urllib.error.URLError: <urlopen error unknown url type: 'https>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Python 3.4.2)当我在脚本中运行 'urllib.request.urlopen(url)' 时出现一个奇怪的错误.如果我直接在 Python 解释器中运行它,它可以正常工作,但当我通过 bash shell (Linux) 在脚本中运行它时就不行了.

我猜它与 'url' 字符串有关,也许是因为我是通过 'string.join' 方法创建的字符串.

导入 urllib.requesturl = "".join((baseurl, other_string, midurl, query))响应 = urllib.request.urlopen(url)

'url' 字符串打印完美,但是当我尝试创建 'response' 字符串时,我得到以下输出:

文件./script.py",第 124 行,在  中响应 = urllib.request.urlopen(url)文件/usr/lib/python3.4/urllib/request.py",第 153 行,在 urlopen返回 opener.open(url, data, timeout)文件/usr/lib/python3.4/urllib/request.py",第 455 行,打开response = self._open(req, data)文件/usr/lib/python3.4/urllib/request.py",第 478 行,在 _open'unknown_open',请求)_call_chain 中的文件/usr/lib/python3.4/urllib/request.py",第 433 行结果 = func(*args)文件/usr/lib/python3.4/urllib/request.py",第 1244 行,在 unknown_openraise URLError('unknown url type: %s' % type)urllib.error.URLError: <urlopen error unknown url type: 'https>

Python 在我的计算机上编译时支持 SSL(这些命令在 Python 解释器中完美运行).

我也试过用 'repr(url)' 和 'str(url)' 包裹 'url' 字符串.我也试过这个:

url = "".join(("'", baseurl, other_string, midurl, query, "'"))

有人知道这是怎么回事吗?

-----编辑-----
我想到了.我的网址中有一个:",我猜 urllib 不喜欢那样.我用%3A"替换了它,现在它可以工作了.

解决方案

你应该使用 urllib.parse.urlencode(), urllib.parse.urljoin(),etc 函数来构造 url 而不是手动加入字符串.它将处理 : -> %3A 转换,例如:

<预><代码>>>>导入 urllib.parse>>>urllib.parse.quote(':')%3A"

(Python 3.4.2) I've got a weird error when I run 'urllib.request.urlopen(url)' inside of a script. If I run it directly in the Python interpreter, it works fine, but not when I run it inside of a script through a bash shell (Linux).

I'm guessing it has something to do with the 'url' string, maybe because I'm creating the string through the 'string.join' method.

import urllib.request
url = "".join((baseurl, other_string, midurl, query))
response = urllib.request.urlopen(url)

The 'url' string prints perfectly, but when I try to create the 'response' string, I get this output:

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>

Python was compiled with SSL support on my computer (these commands work perfectly in the Python interpreter).

I've also tried wrapping the 'url' string with 'repr(url)' and 'str(url)'. I've tried this too:

url = "".join(("'", baseurl, other_string, midurl, query, "'"))

Anyone know what's going on?

-----EDIT-----
I figured it out. My url had a ":" in it, and I guess urllib didn't like that. I replaced it with "%3A" and now it's working.

解决方案

You should use urllib.parse.urlencode(), urllib.parse.urljoin(), etc functions to construct urls instead of manually joining the strings. It would take care of : -> %3A conversion e.g.:

>>> import urllib.parse
>>> urllib.parse.quote(':')
'%3A'

这篇关于urllib.error.URLError: &lt;urlopen error unknown url type: 'https&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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