python3 TypeError: a bytes-like object is required, not ‘str’

Posted in :

在 python3 裡取出某一個url 裡的 html string, 拿來做字串操作,會出現 error:

TypeError: a bytes-like object is required, not 'str'

source code:

import urllib.request
my_html = urllib.request.urlopen(url).read()
my_html=my_html.replace("\r","")

解法:解碼 binary 為 string

read().decode('utf-8')

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *