使用BeautifulSoup从HTML中提取文本

2024-04-19 22:04:17 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在尝试从HTML提取文本使用python中的BeautifulSoup函数-它运行良好,但我没有得到我需要的。我的代码如下:

url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
html = urlopen(url).read()
raw = BeautifulSoup(html).get_text()

Python控制台报告如下,我不理解这个问题,希望能提供帮助。你知道吗

raw = BeautifulSoup(html).get_text()
C:/Users/muradz14/.spyder-py3/raw.py:1: 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 1 of the file C:/Users/muradz14/.spyder-py3/raw.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.

Tags: thetextpyparserurlgetrawhtml
1条回答
网友
1楼 · 发布于 2024-04-19 22:04:17

这只是一个警告。这是很容易解释的,但是代码在不同的解析器中可能会有不同的行为,所以警告是说您可能需要指定您使用的内容。你可以按照它的建议这样做: raw = BeautifulSoup(html, features="lxml").get_text()

注意,有些系统有不同的解析器。对我来说,是features="html.parser"

相关问题 更多 >