【问题标题】:ValueError: 60 columns passed, passed data had 282 columnsValueError:通过了 60 列,传递的数据有 282 列
【发布时间】:2021-11-16 10:02:57
【问题描述】:

他们向我展示了错误ValueError: 60 columns passed, passed data had 282 columns 如何解决这些错误这是页面链接https://www.basketball-reference.com/boxscores/202110190MIL.html 如果您转到点击Q1 的页面并按照图片中显示的那样刮取 1 个表格

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)

【问题讨论】:

    标签: html web-scraping beautifulsoup


    【解决方案1】:

    在这种情况下,直接使用 pandas 阅读页面会更容易:

    tables = pd.read_html(response.text)
    

    这为您提供了 16 个表格,适用于两个团队,包括基本和高级、标题、总计等等。

    【讨论】:

      【解决方案2】:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多