他們向我展示了ValueError: 60 columns passed, passed data had 282 columns如何解決這些錯誤的錯誤,這是頁面鏈接
import requests
from bs4 import BeautifulSoup
import pandas as pd
headers= {'User-Agent': 'Mozilla/5.0'}
#put all item in this array
temp = []
response = requests.get('https://www.basketball-reference.com/boxscores/202110190MIL.html')
soup = BeautifulSoup(response.content, 'html.parser')
table=soup.find('table', class_='sortable stats_table')
headers=[tup.text for tup in table.find_all("th")]
for row in table:
temp.append([row.text for row in table.find_all('td')])
df = pd.DataFrame(temp,columns=headers)
print(df)
uj5u.com熱心網友回復:
在這種情況下,直接使用 Pandas 閱讀頁面會更容易:
tables = pd.read_html(response.text)
這為您提供了 16 個表格,適用于兩個團隊,包括基本和高級、標題、總數等。
uj5u.com熱心網友回復:
beautifulsoup 回答
table=soup.select_one('table#box-BRK-game-basic')
headers=[tup.text for tup in table.select("thead tr:nth-of-type(2) th")]
for row in table.tbody.select('tr:not(.thead)'):
cells=[cell.text for cell in row.children]
temp.append(cells)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361666.html
