【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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章