【发布时间】:2020-02-24 20:19:06
【问题描述】:
我正在使用 BeautifulSoup 从 html 文件中提取内容。 我有几千个提取的 html 文件,并且想要提取所有文件中 p 标签之间的内容。 以下是相关代码:
for line in text:
soup = bs(line, 'html.parser')
autor = soup.find_all('p').text
s = autor.replace('\\n', '')
l.append(s)
我想使用 find_all().text 来提取所有 p 标签之间的文本,但是我收到了这个错误:
ResultSet 对象没有“文本”属性。您可能将项目列表视为单个项目。当你打算调用 find() 时,你调用了 find_all() 吗?
如果我只使用 find().text
autor = soup.find('p').text
我只是得到每个文件的第一个 p 标签。
有人可以帮忙吗?
【问题讨论】:
-
我假设您需要遍历
soup.find('p'),并且对于每个项目,您都会获得text属性。 -
你最好发布 html 示例和预期输出
标签: python beautifulsoup