【发布时间】:2021-09-29 22:14:15
【问题描述】:
我对 HTML 和解析非常陌生,因此如果我为此使用了错误的术语,我深表歉意。我之前问了一个类似的问题,并找到了一些有用的答案。我有以下 HTML sn-p 包含两个表和两个表头(以及更多行但与这篇文章无关)
<body>
<table>
<tr class="header">
<th><strong>Heading 1</strong></th>
<th><strong>Heading 2</strong></th>
<th><strong>Heading 3</strong></th>
<th><p><strong>Heading 4, line 1</strong></p>
<p><strong>Heading 4, line 2</strong></p></th>
</tr>
<tr>
<!--Many more rows-->>
</tr>
</table>
<table>
<tr class="header">
<th><strong>Diff Header 1</strong></th>
<th><strong>Diff Header 2</strong></th>
<th><strong>Diff Header 3</strong></th>
<th><p><strong>Diff Header 4, line 1</strong></p>
<p><strong>Diff Header 4, line 2</strong></p></th>
</tr>
<tr>
<!--Many more rows-->>
</tr>
</table>
</body>
我正在尝试使用 python3.6 和 BeautifulSoup4 来解析它并将文本提取到一个列表中。我的问题是,我希望每个块都有单独的列表。我当前的代码似乎搜索并找到了所有 <th> 标签,而不是第一个表中的标签。
这是我所拥有的:
def parse_html(self):
""" Parse the html file """
with open(self.html_path) as f:
soup = BeautifulSoup(f, 'html.parser')
tables = soup.find_all('table')
for table in tables:
# Find each row in the table
rows = table.find_all_next('tr')
for row in rows:
# Find each column in the row
cols = row.find_all_next('th')
for col in cols:
# Print each cell
print(col) # This is where it seems to be finding every <th>
break # Break just to do the first row (seems not to work?)
问题:如何修改此代码,使其仅在当前行而不是每一行中找到 <th> 标签?
感谢您的帮助!
【问题讨论】:
标签: python html parsing beautifulsoup