如何解析会话的HTML数据,并提取每个说话者的姓名和他/她说的话



我从一个网站上提取了关于不同演讲者之间对话的信息。我提取了字符串形式的信息,其中包含HTML标记。我想摘录一下每位发言者的姓名以及他/她说了些什么。一个示例字符串是:

<p>7 <strong>Assoc Prof Walter Theseira</strong> asked&nbsp;the Deputy Prime Minister and Minister for Finance (a) what has been the framework for Government's procurement of goods and services to address the COVID-19 pandemic; (b) what proportion of such contracts, by dollar value, have been issued through the standard Government procurement system on GeBIZ; (c) what are the considerations when issuing contracts otherwise; and (d) what steps are taken to ensure that all enterprises can participate in such procurement opportunities so as to ensure provision at competitive prices and quality.&nbsp;</p><p class="ql-align-justify"><strong>tThe Second Minister for Finance (Ms Indranee Rajah) (for the Deputy Prime Minister and Minister for Finance)</strong>: Mr Deputy Speaker, t<span style="color: black;">he Government procurement framework calls for open sourcing through the GeBIZ platform as the default approach. However, the use of limited tenders or direct contracting is permitted under specific conditions, such as to protect public health, or for reasons of national security. These practices are aligned with international standards laid out in the World Trade Organisation’s Agreement on Government Procurement.</span></p><p class="ql-align-justify"><span style="color: black;">To address the rapidly evolving COVID-19 situation and avoid further worsening of the public health situation, Government agencies had to obtain necessary goods and services as quickly as possible. While the default approach continues to be open sourcing via GeBIZ, the urgency meant that, in some cases, it was not practical to do so. For such instances, the procedures under Emergency Procurement allow Government agencies to directly contract with suppliers who have the necessary expertise and resources, instead of going through open sourcing. For example, as we needed to quickly source for and fit out premises to house at-risk persons, and also secure essential medical supplies, the agencies concerned established direct contracts with the suppliers outside GeBIZ who were best able to meet the requirements within the shortest timeframe possible. </span></p><p class="ql-align-justify"><span style="color: black;">Similar Emergency Procurement practices are also adopted in other jurisdictions. For example, in Australia, the European Union, New Zealand, the United Kingdom and the United States, government agencies may directly award a contract without the need for open competition under emergency situations, such as the COVID-19 crisis. </span></p>

我想摘录一下Walter Theseira副教授所说的文字内容。这将是:

询问副总理兼财政部长(a(为应对新冠肺炎疫情,政府采购商品和服务的框架是什么;(b( 按照美元价值计算,通过GeBIZ的标准政府采购系统签发的此类合同的比例是多少;(c( 以其他方式发布合同时有哪些考虑因素;(d(采取了哪些措施确保所有企业都能参与此类采购机会,以确保以有竞争力的价格和质量提供

其他发言者的发言也是如此。然后,我将相应地存储信息(演讲者和各自的内容(以供进一步分析。

我尝试过regex,但考虑到我必须考虑的各种HTML标记,它似乎不够灵活和可扩展。我想知道python还有什么其他选择。

我会使用BeautifulSoup python来完成这样的任务。

下面是一个使用html的快速示例。

from bs4 import BeautifulSoup
html = """
<p>7 <strong>Assoc Prof Walter Theseira</strong> asked&nbsp;the Deputy Prime Minister and Minister for Finance (a) what
has been the framework for Government's procurement of goods and services to address the COVID-19 pandemic; (b) what
proportion of such contracts, by dollar value, have been issued through the standard Government procurement system
on GeBIZ; (c) what are the considerations when issuing contracts otherwise; and (d) what steps are taken to ensure
that all enterprises can participate in such procurement opportunities so as to ensure provision at competitive
prices and quality.&nbsp;</p>
<p class="ql-align-justify"><strong>tThe Second Minister for Finance (Ms Indranee Rajah) (for the Deputy Prime
Minister and Minister for Finance)</strong>: Mr Deputy Speaker, t<span style="color:black;">he Government
procurement framework calls for open sourcing through the GeBIZ platform as the default approach. However, the
use of limited tenders or direct contracting is permitted under specific conditions, such as to protect public
health, or for reasons of national security. These practices are aligned with international standards laid out
in the World Trade Organisation’s Agreement on Government Procurement.</span></p>
<p class="ql-align-justify"><span style="color: black;">To address the rapidly evolving COVID-19 situation and avoid
further worsening of the public health situation, Government agencies had to obtain necessary goods and services
as quickly as possible. While the default approach continues to be open sourcing via GeBIZ, the urgency meant
that, in some cases, it was not practical to do so. For such instances, the procedures under Emergency
Procurement allow Government agencies to directly contract with suppliers who have the necessary expertise and
resources, instead of going through open sourcing. For example, as we needed to quickly source for and fit out
premises to house at-risk persons, and also secure essential medical supplies, the agencies concerned
established direct contracts with the suppliers outside GeBIZ who were best able to meet the requirements within
the shortest timeframe possible. </span></p>
<p class="ql-align-justify"><span style="color: black;">Similar Emergency Procurement practices are also adopted in
other jurisdictions. For example, in Australia, the European Union, New Zealand, the United Kingdom and the
United States, government agencies may directly award a contract without the need for open competition under
emergency situations, such as the COVID-19 crisis. </span></p>
"""

soup = BeautifulSoup(html, 'html.parser')

for i in soup.find_all("strong") :
print(i.text)


输出如下:

Assoc Prof Walter Theseira
The Second Minister for Finance (Ms Indranee Rajah) (for the Deputy Prime
Minister and Minister for Finance)


这是文件:

https://www.crummy.com/software/BeautifulSoup/bs4/doc/

最新更新