[問題] 求救 ...困在編碼問題

作者: imimi (im)   2016-08-30 22:03:44
想請教各位高手
我有個input.txt 我很確定他是用utf-8儲存的
但是用以下的beautiful soup 處理 strip tag
卻還是亂碼 想請問是不是哪裡寫錯了 謝謝
import requests
from bs4 import BeautifulSoup
res = requests.get('http://localhost/input.txt',verify=False)
obj=bytes(res.text, "utf-8")
UTF8str=obj.decode('utf-8', 'ignore')
soup = BeautifulSoup(UTF8str, "html.parser")
text = soup.getText()
print(text)
但是出來 還是亂碼
我是照著書打的 實在不知道問題出在哪
感謝各位高手 ....orz
作者: Neverfor (八卦蟑螂)   2016-08-30 22:19:00
討厭win10記事本
作者: enjoyloli (M)   2016-08-31 00:07:00
#-*- coding: utf-8 -*- 有用嗎?
作者: uranusjr (←這人是超級笨蛋)   2016-08-31 05:50:00
既然文字內容就是 UTF-8, 你為什麼要 encode 又 decode直接把 res.text 拿去 parse 不就好了?

Links booklink

Contact Us: admin [ a t ] ucptt.com