bs4 '4.8.2'版本html解析錯誤(使用findall方法查找link標籤,返回中包含style標籤)

大概率因爲頁面存在註釋,例如:

<!--[if lte IE 8]>
<style index="index" data-compress="strip">
.s_form{top:260px}
</style>
<![endif]-->
<!--[if IE 8]>
<style index="index" data-compress="strip">
#u1 a.mnav,#u1 a.mnav:visited,#u1 a.lb,#u1 a.lb:visited,#u1 a.pf,#u1 a.pf:visited,#u1 a.bri,#u1 a.bri:visited{font-family:simsun;}
</style>
<![endif]-->

 

所以返回的列表中錯誤包含別的標籤,而不是查找的標籤。

但是標籤的個數和頁面對得上。

html.parser需要背這個鍋。

使用html5lib,可以正常解析。但需要額外安裝,pip install html5lib

beautifulsoup(markup,'html5lib')

lxml庫也可以解決這個問題,但安裝較慢比較拼人品。pip install lxml

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章