問題:python3 使用beautifulSoup時,出錯UnicodeDecodeError: 'gbk' codec …….


想將html文件轉為純文本,用Python3調用beautifulSoup

超簡單的代碼一直出錯,用於打開本地文件:

 
 
 
         
  1. from bs4 import BeautifulSoup
  2. file = open('index.html')
  3. soup = BeautifulSoup(file,'lxml')
  4. print (soup)

出現下面的錯誤

UnicodeDecodeError : ‘gbk’ codec can’t decode byte 0xff in position 0: illegal multibyte sequence

beautifulSoup不是自稱可以解析各種編碼格式的嗎?為什么還會出現解析的問題???

搜了很多關於beautifulSoup的都沒有解決,突然發現,如果把代碼寫成

 
 
 
         
  1. from bs4 import BeautifulSoup
  2. file = open('index.html')
  3. str1 = file.read() # 錯誤出在這一行!!!
  4. soup = BeautifulSoup(str1,'lxml')
  5. print (soup)

原來如此! 問題出在文件讀取而非BeautifulSoup的解析上!!

好吧,查查為什么文件讀取有問題,直接上正解,同樣四行代碼

 
 
 
         
  1. from bs4 import BeautifulSoup
  2. file = open('index.html','r',encoding='utf-16-le')
  3. soup = BeautifulSoup(file,'lxml')
  4. print (soup)

然后soup.get_text()得到標簽中的文字

其它

如果文件中存在多種編碼而且報錯,可以采用下面這種方式忽略,沒測試–

 
 
 
         
  1. soup = BeautifulSoup(content.decode('utf-8','ignore'))





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM