“如何从BeautifulSoup中带有ID的表获取数据?

问题描述 投票:0回答:1

我正在尝试使用 BeautifulSoup 和 requests 库从 id='stats_standard' 的表中获取数据,但我尝试了各种方法,例如使用 find 和 select,但我仍然收到 None 或 []。有没有人有解决办法?

import requests 
from bs4 import BeautifulSoup

url = 'https://fbref.com/en/comps/9/2023-2024/stats/2023-2024-Premier-League-Stats' 

page = requests.get(url) 

soup = BeautifulSoup(page.text, 'html.parser')  

table = soup.find('table', id='stats_standard')
print(table) # None

我想从表中获取数据

python web-scraping beautifulsoup
1个回答
0
投票

有两张表,都不是你要找的ID:

import requests 
from bs4 import BeautifulSoup

url = 'https://fbref.com/en/comps/9/2023-2024/stats/2023-2024-Premier-League-Stats' 

page = requests.get(url) 

soup = BeautifulSoup(page.text, 'html.parser')  

tables = soup.find_all('table')
for table in tables:
    print(table.attrs)

输出:

{'class': ['stats_table', 'sortable', 'min_width'], 'id': 'stats_squads_standard_for', 'data-cols-to-freeze': ',1'}
{'class': ['stats_table', 'sortable', 'min_width'], 'id': 'stats_squads_standard_against', 'data-cols-to-freeze': ',1'}
© www.soinside.com 2019 - 2024. All rights reserved.