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

看板Python作者 (im)時間7年前 (2016/08/30 22:03), 7年前編輯推噓2(203)
留言5則, 4人參與, 最新討論串1/1
想請教各位高手 我有個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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.217.14.99 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1472565827.A.D8A.html

08/30 22:19, , 1F
討厭win10記事本
08/30 22:19, 1F
※ 編輯: imimi (180.217.14.99), 08/30/2016 22:26:02

08/31 00:07, , 2F
#-*- coding: utf-8 -*- 有用嗎?
08/31 00:07, 2F

08/31 05:50, , 3F
既然文字內容就是 UTF-8, 你為什麼要 encode 又 decode
08/31 05:50, 3F

08/31 05:50, , 4F
直接把 res.text 拿去 parse 不就好了?
08/31 05:50, 4F

03/10 17:39, , 5F
感謝感謝~~ 理解了~~~
03/10 17:39, 5F
文章代碼(AID): #1NnP93sA (Python)