(Python 3.4.2) 當我在腳本中運行'urllib.request.urlopen(url)'時出現奇怪的錯誤。如果我直接在Python解釋器中運行它,它可以正常工作,但不是當我通過bash shell(Linux)在腳本中運行它時。urllib.error.URLError:<urlopen錯誤未知的url類型:'https>
我猜它與'url'字符串有關,也許是因爲我通過'string.join'方法創建了字符串。
import urllib.request
url = "".join((baseurl, other_string, midurl, query))
response = urllib.request.urlopen(url)
「網址」字符串打印完美的,但是當我嘗試創建「響應」的字符串,我得到這樣的輸出:
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與我的計算機上的SSL支持編譯(這些命令在Python解釋器中完美工作)。
我也嘗試用'repr(url)'和'str(url)'包裝'url'字符串。我也試過這個:
url = "".join(("'", baseurl, other_string, midurl, query, "'"))
任何人都知道發生了什麼事?
-----編輯-----
我想通了。我的網址中有一個「:」,我想urllib不喜歡那個。我用「%3A」取代了它,現在它正在工作。
另請參見[urllib的HTTPS請求:<的urlopen錯誤未知URL鍵入:https>](https://stackoverflow.com/q/28376506/608639) ,[urllib無法讀取https](https://stackoverflow.com/q/27208131/608639),[urllib.error.URLError:](https://stackoverflow.com/ q/27115803/608639),[urllib HTTPS請求:](https://stackoverflow.com/q/44750732/608639)等 –
jww
2018-02-08 11:37:55