要从表格中获取链接,可以使用BeautifulSoup的find_all()方法来找到所有的表格行,然后循环遍历每一行,再使用find_all()方法找到每一行中的链接。
以下是一个示例代码:
from bs4 import BeautifulSoup
# 假设html是包含表格的HTML代码
html = '''
Link 1
Link 2
Link 3
Link 4
'''
# 创建BeautifulSoup对象
soup = BeautifulSoup(html, 'html.parser')
# 找到所有的表格行
rows = soup.find_all('tr')
# 遍历每一行
for row in rows:
# 找到每一行中的链接
links = row.find_all('a')
for link in links:
print(link['href'])
这个代码会输出所有的链接:
http://example.com/link1
http://example.com/link2
http://example.com/link3
http://example.com/link4
这样就可以从表格中获取到链接了。