从中提取文本 元素结束 分子

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

我正在编写一个使用BeautifulSoup<p>元素中提取文本的脚本;它运行良好,直到我遇到包含<p>标签的<br>元素,在这种情况下,它只捕获第一个<br>标签之前的文本。如何编辑我的代码以捕获所有文本?

我的代码:

coms = soup.select('li > div[class=comments]')[0].select('p')
inp = [i.find(text=True).lstrip().rstrip() for i in coms]

问题HTML(注意<br>标签):

<p>             
                    Alts called now through 53. No more will be called til the 12:50 group. EMCs are still on the table to be seen.<br>
<br>
ITR info:<br>
<br>
Rachel Hoffman, CD<br>
Chris Kory, acc.<br>
<br>
Monitor is Iftiaz Haroon.                </p>

我的代码目前输出的内容:

>> 'Alts called now through 53. No more will be called til the 12:50 group. EMCs are still on the table to be seen.'

我的代码应该输出什么(注意额外的文本):

>> 'Alts called now through 53. No more will be called til the 12:50 group. EMCs are still on the table to be seen. ITR info: Rachel Hoffman, CD Chris Kory, acc. Monitor is Iftiaz Haroon.'

(注意:原谅我有时候有问题的术语;我基本上是自学成才。)

python html web-scraping beautifulsoup
2个回答
0
投票

我担心这个问题可能是错误的。我将HTML复制到一个文件中,然后运行以下代码:

>>> import bs4
>>> soup = bs4.BeautifulSoup(open('matthew.htm').read(), 'lxml')
>>> soup.find('p').text
'             \n                    Alts called now through 53. No more will be called til the 12:50 group. EMCs are still on the table to be seen.\n\nITR info:\n\nRachel Hoffman, CD\nChris Kory, acc.\n\nMonitor is Iftiaz Haroon.                '

显然,恢复所需文本是一件简单的事情。


0
投票

你可以使用get_text(strip=True)

从文档:

如果您只想要文档或标记的文本部分,则可以使用get_text()方法。它返回文档中或标记下的所有文本,作为单个Unicode字符串。

你可以告诉Beautiful Soup使用strip=True从每一段文本的开头和结尾去掉空格。

html = '''<p>             
                    Alts called now through 53. No more will be called til the 12:50 group. EMCs are still on the table to be seen.<br>
<br>
ITR info:<br>
<br>
Rachel Hoffman, CD<br>
Chris Kory, acc.<br>
<br>
Monitor is Iftiaz Haroon.                </p>'''

soup = BeautifulSoup(html, 'lxml')
print(soup.find('p').get_text(strip=True))

输出:

Alts called now through 53. No more will be called til the 12:50 group. EMCs are still on the table to be seen.ITR info:Rachel Hoffman, CDChris Kory, acc.Monitor is Iftiaz Haroon.
© www.soinside.com 2019 - 2024. All rights reserved.