以下是我的代码:
import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
stats_page = requests.get('https://www.sports-reference.com/cbb/schools/loyola-il/2020.html')
content = stats_page.content
soup = BeautifulSoup(content, 'html.parser')
table = soup.find(name='table', attrs={'id':'per_poss'})
html_str = str(table)
df = pd.read_html(html_str)[0]
df.head()
Run Code Online (Sandbox Code Playgroud)
我得到错误: ValueError: No tables found.
但是,当我attrs={'id':'per_poss'}使用不同的表 ID 进行交换时 ,就像 attrs={'id':'per_game'}得到输出一样。
我不熟悉 html 和抓取,但我注意到在工作表中,这是 html: <table class="sortable stats_table now_sortable is_sorted" id="per_game" data-cols-to-freeze="2">
在不起作用的表格中,这是 html: <table class="sortable stats_table now_sortable sticky_table re2 le1" id="totals" data-cols-to-freeze="2">
似乎表类不同,我不确定这是否是导致此问题的原因以及如何解决。
谢谢!