BeautifulSoup库未写明解析器警告

发布时间:2020-08-02 07:20:01 作者:莫渺1996
来源:网络 阅读:4638
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print(bsObj.h2)

代码运行之后警告如下:
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 4 of the file D:/Python/venv/test8.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.

翻译如下:
用户警告:没有显式指定语法分析器,因此我使用了此系统的最佳可用HTML语法分析器(“lxml”)。这通常不是问题,但是如果您在另一个系统上运行此代码,或者在不同的虚拟环境中运行此代码,它可能会使用不同的解析器并表现出不同的行为。

导致此警告的代码位于文件d:/python/venv/test8.py的第4行。要消除此警告,请将附加参数'features=“lxml”'传递给beautifulsoup构造函数。

解决:指定解析器,一般使用'lxml'

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read(),'lxml')
print(bsObj.h2)
推荐阅读:
  1. python 爬虫之BeautifulSoup 库的基本使用
  2. Python爬虫库BeautifulSoup怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

beautifulsoup 解析器 警告

上一篇:Oracle 学习之 11g Clone 安装

下一篇:jquery 时间对比

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》