在下面的示例中,我想读取相应的标题<h1>
和段落<p>
数据...
我有许多相互关联的标题和段落,因此,如果找到标题,则需要提取相应的段落数据:
<h1>Supplementary Materials </h1>\n
<p />\n
<p>The workshop entitled “Next generation MRA (Microbiological Risk Assessment); integration of Omics data into assessment” took place in Athens, Greece, May 13-14, 2016, and resulted in four papers that are published in this issue, namely, Cocolin et al., Rantsiou et al., Den Besten et al., and Haddad et al. </p>\n
<h1>Testing data</h1>
<p>The supplementary materials, Table S1 and Table S2, are integrated parts of these four papers.</p>\n
<p />
<h1>Supplementary Materials </h1>\n
<p />\n
<p>The workshop entitled “Next generation MRA (Microbiological Risk Assessment); integration of Omics data into assessment” took place in Athens, Greece, May 13-14, 2016, and resulted in four papers that are published in this issue, namely, Cocolin et al., Rantsiou et al., Den Besten et al., and Haddad et al. </p>\n
<h1>Testing data</h1>
<p>The supplementary materials, Table S1 and Table S2, are integrated parts of these four papers.</p>\n
<p />
答案 0 :(得分:0)
html是否真的像这样重复,还是错别字?
html = '''<h1>Supplementary Materials </h1>\n
<p />\n
<p>The workshop entitled “Next generation MRA (Microbiological Risk Assessment); integration of Omics data into assessment” took place in Athens, Greece, May 13-14, 2016, and resulted in four papers that are published in this issue, namely, Cocolin et al., Rantsiou et al., Den Besten et al., and Haddad et al. </p>\n
<h1>Testing data</h1>
<p>The supplementary materials, Table S1 and Table S2, are integrated parts of these four papers.</p>\n
<p />
<h1>Supplementary Materials </h1>\n
<p />\n
<p>The workshop entitled “Next generation MRA (Microbiological Risk Assessment); integration of Omics data into assessment” took place in Athens, Greece, May 13-14, 2016, and resulted in four papers that are published in this issue, namely, Cocolin et al., Rantsiou et al., Den Besten et al., and Haddad et al. </p>\n
<h1>Testing data</h1>
<p>The supplementary materials, Table S1 and Table S2, are integrated parts of these four papers.</p>\n
<p /> '''
import bs4
soup = bs4.BeautifulSoup(html, 'html.parser')
heads = soup.find_all('h1')
for head in heads:
para = head.find_next('p', text=True).text
print ('Header: %s\nParagraph: %s\n' %(head.text, para))
输出:
Header: Supplementary Materials
Paragraph: The workshop entitled “Next generation MRA (Microbiological Risk Assessment); integration of Omics data into assessment” took place in Athens, Greece, May 13-14, 2016, and resulted in four papers that are published in this issue, namely, Cocolin et al., Rantsiou et al., Den Besten et al., and Haddad et al.
Header: Testing data
Paragraph: The supplementary materials, Table S1 and Table S2, are integrated parts of these four papers.
Header: Supplementary Materials
Paragraph: The workshop entitled “Next generation MRA (Microbiological Risk Assessment); integration of Omics data into assessment” took place in Athens, Greece, May 13-14, 2016, and resulted in four papers that are published in this issue, namely, Cocolin et al., Rantsiou et al., Den Besten et al., and Haddad et al.
Header: Testing data
Paragraph: The supplementary materials, Table S1 and Table S2, are integrated parts of these four papers.