錯誤消息是自我解釋的:輸入字符串中有一個字節0xf0,該字符串應該是一個ascii字符串。
您應該給出確切的錯誤消息以及它發生了什麼行,但我可以猜到發生在info = urllib.parse.parse_qs(source_code)
上,因爲parse_qs
需要unicode字符串或ascii字節字符串。
第一個問題是,爲什麼你叫parse_qs
數據來自YouTube的到來,因爲Python的斯坦達特庫的醫生說:
Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.
所以你要分析這個對=
和&
性格來解釋它作爲查詢字符串形式key1=value11&key2=value2&key1=value12
給予{ 'key1': [ 'value11', 'value12'], 'key2': ['value2']}
。
如果你知道爲什麼你想要的,你應該先字節串解碼成unicode字符串,使用正確的編碼,或者如果不確定Latin1
這是能夠接受任何字節:
def start(url):
source_code = urllib.request.urlopen(url).read().decode('latin1')
info = urllib.parse.parse_qs(source_code)
print(info)
感謝你這麼很多,它的工作 –