[英]How to parse a htm file using Beautiful soup
我正在嘗試使用BeautifulSoup解析htm本地文件。
.htm是文件類型。
from bs4 import BeautifulSoup
with open('locfile.htm') as fp:
soup = BeautifulSoup(fp, "html5lib")
print(soup)
嘗試使用三個不同的解析器,但獲得相同的結果。 html5lib的示例
<html><body><p>t a b l e i d = " T a b l a D a t a " c l a s s = " T a b l a w i d t h = " 9 0 % >
t r >....
.....
等等。 我認為“&gt”是但已轉換為那些字符串。
使用html.parser
和html5llib
獲得類似的結果
如何將標簽保持在體內?
可能是錯誤的解析操作?
soup.contents
[<html><head></head><body><table id=........
..................
</body></html>
但內部標簽已丟失,或轉換為html轉義字符
如何維護標簽?
終於我找到了解決方法。
問題是原始文件的編碼:
with open('locfile.htm',encoding="utf-16LE") as fp:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.