python爬虫问题-BeautifulSoup库运行提示GuessedAtParserWarning: No parser was explicitly specified

本文介绍了如何在Python中使用BeautifulSoup遇到GuessedAtParserWarning时,通过明确指定'html.parser'解析器来避免警告。通过实际代码示例,展示了如何修改BeautifulSoup构造函数以确保代码在不同环境中的稳定性。

开发工具

  1. python版本: python-3.8.1-amd64
  2. python开发工具: JetBrains PyCharm 2018.3.6 x64
  3. 安装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())
<head>
<script>
		location.replace(location.href.replace("https://","http://"));
	</script>
</head>

解决方案

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)

运行效果

<head>
<script>
		location.replace(location.href.replace("https://","http://"));
	</script>
</head>

总结

提示信息:未明确指定解析器,因此我正在为此系统使用最佳的HTML解析器(“ html.parser”),在调用BeautifulSoup指定html.parser即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值