我对熊猫和美女队还不熟悉,我正在尝试跟随《梦幻足球》,并利用《梦幻足球》中的数据创建我自己的梦幻篮球(NBA联盟)。 它总是给我这个错误:
ValueError: 0 columns passed, passed data had 3 columns
我认为这意味着我的数据中有3列,但没有一列被迭代(或者我已经退出)。我试着在这里查找类似的问题,但我一直无法解决这个问题
代码如下:
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
import numpy as np
import math
# NBA season we using (2020)
year = 2020
# url we scraping data from
url = 'https://fantasydata.com/nba/fantasy-basketball-leaders?scope=1&season=2020&seasontype=3&conference=1&scoringsystem=1&date=09-27-2020'.format(year)
# use this to open the url
html = urlopen(url)
soup = bs(html, 'html5lib')
# use findAll() to get column headers
soup.findAll('tr', limit=2)
# use getText() to extract the text we need
headers = [th.getText() for th in soup.findAll('tr', limit=2)[0].findAll('th')]
# exclude first column
headers = headers[1:]
# first row is a no no
rows = soup.findAll('tr')[1:]
player_stats = [[td.getText() for td in rows[i].findAll('td')] for i in range(len(rows))]
df = pd.DataFrame(player_stats, columns=headers)
df.head(529)
不需要使用硒。只需获取ajax json响应并转换为数据帧。它是一个嵌套的json,因此您可能需要根据需要做更多的工作,但它是为您准备的:
输出:
相关问题 更多 >
编程相关推荐