可以使用Beautiful Soup的.find_all()方法来获取表格中的数据。例如,对于以下的HTML代码:
姓名
年龄
张三
25
李四
30
可以使用以下的Python代码来解析表格中的数据:
from bs4 import BeautifulSoup
html = """
姓名
年龄
张三
25
李四
30
"""
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table')
rows = table.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [col.text.strip() for col in cols]
print(cols)
输出结果为:
[]
['张三', '25']
['李四', '30']
这样就可以从表格中获取数据了。