简体   繁体   中英

Python BeautifulSoup: parsing multiple tables with same table is

I am having to use BeautifulSoup to parse a table, but there are several tables with the same name. See the example below and in this case I want table #6 which has "First goal stats:..."

enter image description here

page

site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, 'html.parser')
tb = soup.findAll('table',{'id': 'btable'})
table_str = str(tb)
df = pd.read_html(table_str)[0]

tb is just a list contains all the tables and you can get your table by index.. I think your target table has the index of 4.

tb = soup.findAll('table',{'id': 'btable'})
table_str = str(tb[4]) #select only one table by its index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM