BeautifulSoup警告: BeautifulSoup([your markup], "html.parser")

今天看<python数据采集>一书,跟着敲了点代码,代码如下:

from urllib.request import urlopen
from bs4 import BeautifulSoup


html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print(bsObj.h1)

这个书中没提,但是会报警告

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/jiangxiaohan/Desktop/PythonDemo/beautifulSoupDemo/beautifulSoupTest1.py
/Users/jiangxiaohan/Library/Python/3.6/lib/python/site-packages/bs4/__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). 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 6 of the file /Users/jiangxiaohan/Desktop/PythonDemo/beautifulSoupDemo/beautifulSoupTest1.py. To get rid of this warning, change code that looks like this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "html.parser")

  markup_type=markup_type))
<h1>An Interesting Title</h1>

Process finished with exit code 0

这个就是说你没有指定beautifulsoup的解析器,所以作者默认使用html.parser来解析,一般没什么问题,但是如果运行在其它系统或环境它可能会使用不同的解析器(可能会导致不同的结果)。如果想消除这个警告信息你可以这样写

BeautifulSoup(html.read(), "html.parser")

这样就好了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 初学Python,照 Python网络数据采集 这个本的代码打的时候遇到的一个警告 尴尬看不懂,谷歌百度了一下,大...
    名为信念的旗帜阅读 1,153评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,399评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,890评论 19 139
  • 假如婚姻欺骗了你 所谓的幸福如烟云飘过后 转瞬即消失无影 生活在繁复与单调之间 不断的轮换 假如婚姻欺骗了你 所谓...
    汪小瑞阅读 562评论 0 0
  • 突想写几首小短诗, 1 蓝色是我最爱的颜色 蓝色是我最爱的颜色 淡蓝的天空 翠蓝的大海 蓝紫色的蝴蝶花 吹进春季里...
    _美朵阅读 407评论 0 0

友情链接更多精彩内容