【beautifulsoup】python標準庫解析器解析網頁問題解決

使用python標準庫解析(即BeautifulSoup(page, "html.parse"))出現網頁解析問題

如下,頁面明明沒有結束,</html>卻提前出現了,導致有些text沒有了原本與之對應的標籤,也就取不到了


在嘗試了很多種方法之後,發現是解析器的問題,所以更換了解析器

BeautifulSoup(page, "html5lib")

from bs4 import BeautifulSoup
import urllib.request
import logging

logging.basicConfig(level=logging.DEBUG)


def parse(url):
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page, "html5lib")
    _desc_soup = soup.find("div", class_="det-app-data-info")
    logging.debug("description is %s", _desc_soup.get_text().strip())

使用html5lib解析器需要安裝這個model

pip install html5lib


安裝之後還是運行會報錯,崩潰。。。

module 'html5lib.treebuilders' has no attribute '_base'

其實升級一下html5lib就可以了,升級命令是:

pip install --upgrade html5lib==1.0b8。


現在可以正常運行了~

發佈了48 篇原創文章 · 獲贊 47 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章