从python中的网页中提取所有表

时间:2018-06-18 12:01:12

标签: python pandas web-scraping

我想从python中获取网页中的所有表格,但我的代码只显示了一个表格而不是全部。请帮忙。

提前致谢。

import pandas as pd
import requests
from bs4 import BeautifulSoup
from tabulate import tabulate

res = requests.get("http://www.findchips.com/search/PCB")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[0]
df = pd.read_html(str(table))
print( tabulate(df[0], headers='keys', tablefmt='psql') )

1 个答案:

答案 0 :(得分:0)

使用此:

tables = soup.find_all('table')
for table in tables
# do your stuff for every table
df = pd.read_html(str(table))
print( tabulate(df[0], headers='keys', tablefmt='psql') )