禁止beautifulsoup中的url警告

问题描述 投票:0回答:2

我正在使用 Beautiful Soup 4 来解析一些从互联网上抓取的 html 格式的文本。有时,该文本只是某些网站的链接。 BS4 非常生气的事实:

UserWarning: "http://example.com" looks like a URL. Beautiful Soup is not
an HTTP client. You should probably use an HTTP client to get the document
behind the URL, and feed that document to Beautiful Soup.

我非常清楚这个事实,我只是想解释文本输入,而不是听讲座。我使用控制台来监视脚本的活动,它被一个非常愤怒的库弄乱了。

有什么方法可以抑制或禁用此警告吗?

python beautifulsoup
2个回答
47
投票

简单地抑制警告并继续处理此作品:

import warnings
from bs4 import UserWarning
warnings.filterwarnings("ignore", category=UserWarning, module='bs4')

0
投票

从 beautifulsoup4 v4.9.3(2021 年 6 月 1 日发布)开始,此警告已转变为新的子类

MarkupResemblesLocatorWarning
。我还发现将模块过滤为
bs4
不再有效。这是从 beautifulsoup4 v4.12.3 开始运行的调用:

from bs4 import MarkupResemblesLocatorWarning
warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning)
© www.soinside.com 2019 - 2024. All rights reserved.