要在BeautifulSoup中获取表格中的
from bs4 import BeautifulSoup
html = """
Cell 1
Cell 2
Cell 3
Cell 4
"""
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table')
rows = table.find_all('tr')
for row in rows:
cells = row.find_all('td')
for cell in cells:
print(cell.text)
这段代码首先创建了一个包含表格的HTML字符串。然后,使用BeautifulSoup将其解析为一个BeautifulSoup对象。接下来,使用.find()方法找到表格标签,并使用.find_all()方法找到所有的
运行以上代码,将输出以下结果:
Cell 1
Cell 2
Cell 3
Cell 4
这证明了BeautifulSoup可以成功获取表格中的