- 开发工具
- 问题描述
- 问题代码
- 运行结果
- 解决方案
- 运行效果
- 总结
- python版本: python-3.8.1-amd64
- python开发工具: JetBrains PyCharm 2018.3.6 x64
- 安装BeautifulSoup库(指定阿里镜像安装会很快)
pip install beautifulsoup4 -i http://mirrors.aliyun.com/pypi/simple/
问题代码GuessedAtParserWarning: 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.
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("https://www.baidu.com/s?wd=python%E7%88%AC%E8%99%AB%E4%B9%A6%E7%B1%8D")
bsObj = BeautifulSoup(html.read())
print(bsObj.head)
运行结果
GuessedAtParserWarning: 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 5 of the file To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.
bsObj = BeautifulSoup(html.read())
解决方案
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("https://www.baidu.com/s?wd=python%E7%88%AC%E8%99%AB%E4%B9%A6%E7%B1%8D")
# 在调用的BeautifulSoup指定html.parser即可
bsObj = BeautifulSoup(html.read(), 'html.parser')
print(bsObj.head)
运行效果
总结
提示信息:未明确指定解析器,因此我正在为此系统使用最佳的HTML解析器(“ html.parser”),在调用BeautifulSoup指定html.parser即可



