天天看點

python urllib.request.urljoin_python3 urllib.request.urlopen() 位址打開錯誤

錯誤内容:UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-29: ordinal not in range(128)

1.以為是代碼錯誤,檢查tab符,并沒有問題,

2.将代碼粘貼到空白項目中去,發現還是不對。

3.百度:http://blog.csdn.net/olanlanxiari/article/details/48201231

1.Python在安裝時,預設的編碼是ascii,當程式中出現非ascii編碼時,python的處理常常會報這樣的錯UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),python沒辦法處理非ascii編碼的,此時需要自己設定将python的預設編碼,一般設定為utf8的編碼格式。

2.将上面問題自身檢查,我這不存在這種問題,是以繼續百度

3.發現python3 urlopen()連結位址不能出現中文,,那麼有中文怎麼辦?

a.辦法總是有的:替換

b.将中文内容通過quote方法替換

from urllib.parse import quote

quote(url)

quote(url,safe='/:?=')

c.quote可用的參數如下:

quote(string,safe='/',encoding=None,errors=None)

reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","