我正在学习Python和BeautifulSoup,用于从网页上爬取数据并读取HTML表格。我可以将其读入Open Office,它会显示这是第11个表格。
似乎BeautifulSoup是首选,但有人能告诉我如何获取特定的表格和所有行吗?我已经查看了模块文档,但无法理解它。我在网上找到的许多示例似乎做得比我需要的更多。
我正在学习Python和BeautifulSoup,用于从网页上爬取数据并读取HTML表格。我可以将其读入Open Office,它会显示这是第11个表格。
似乎BeautifulSoup是首选,但有人能告诉我如何获取特定的表格和所有行吗?我已经查看了模块文档,但无法理解它。我在网上找到的许多示例似乎做得比我需要的更多。
如果你有一个需要解析的HTML块,使用BeautifulSoup库应该是相当简单的。基本思路是使用findChildren
方法找到表格,然后使用string
属性获取单元格内的文本值。
>>> from BeautifulSoup import BeautifulSoup
>>>
>>> html = """
... <html>
... <body>
... <table>
... <th><td>column 1</td><td>column 2</td></th>
... <tr><td>value 1</td><td>value 2</td></tr>
... </table>
... </body>
... </html>
... """
>>>
>>> soup = BeautifulSoup(html)
>>> tables = soup.findChildren('table')
>>>
>>> # This will get the first (and only) table. Your page may have more.
>>> my_table = tables[0]
>>>
>>> # You can find children with multiple tags by passing a list of strings
>>> rows = my_table.findChildren(['th', 'tr'])
>>>
>>> for row in rows:
... cells = row.findChildren('td')
... for cell in cells:
... value = cell.string
... print("The value in this cell is %s" % value)
...
The value in this cell is column 1
The value in this cell is column 2
The value in this cell is value 1
The value in this cell is value 2
>>>
html = '''<table>
<tr>
<td>Top level table cell</td>
<td>
<table>
<tr><td>Nested table cell</td></tr>
<tr><td>...another nested cell</td></tr>
</table>
</td>
</tr>
</table>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
non_nested_tables = [t for t in soup.find_all('table') if not t.find_all('table')]
另外,如果您想提取所有表格内容,包括嵌套其他表格的表格,您可以仅提取顶级tr
和th
/td
标题。为此,在调用find_all
方法时,需要关闭递归:
soup = BeautifulSoup(html, 'lxml')
tables = soup.find_all('table')
cnt = 0
for my_table in tables:
cnt += 1
print ('=============== TABLE {} ==============='.format(cnt))
rows = my_table.find_all('tr', recursive=False) # <-- HERE
for row in rows:
cells = row.find_all(['th', 'td'], recursive=False) # <-- HERE
for cell in cells:
# DO SOMETHING
if cell.string: print (cell.string)
输出:
=============== TABLE 1 ===============
Top level table cell
=============== TABLE 2 ===============
Nested table cell
...another nested cell
如果您没有嵌套表格,递归是一个很好的技巧,但如果您有嵌套表格,则需要一次处理一级。
可能会让您感到困扰的一个HTML变化是以下情况,其中也使用了tbody和/或thead元素。
html = '
<table class="fancy">
<thead>
<tr><th>Nested table cell</th></tr>
</thead>
<tbody>
<tr><td><table id=2>...another nested cell</table></td></tr>
</tbody>
</table>
</table>
table = soup.find_all("table", {"class": "fancy"})[0]
thead = table.find_all('thead', recursive=False)
header = thead[0].findChildren('th')
tbody = table.find_all('tbody', recursive=False)
rows = tbody[0].find_all('tr', recursive=False)
现在你有表头和数据行
.findChildren(['th', 'tr'])
是在查找带有th
或tr
标签类型的元素。如果您只想查找tr
元素,则应该使用.findChildren('tr')
(注意不是列表,只是一个字符串)。 - JJ Geewaxth
是一个表头单元格。这是一个格式不正确的表格。 - Andriy Makukha