date = '2017-08-04'
writer = pd.ExcelWriter('MLB Daily Data.xlsx')
url_4 = 'http://www.baseballpress.com/lineups/'+date
resp_4 = requests.get(url_4)
soup_4 = BeautifulSoup(resp_4.text, "lxml")
lineups = soup_4.findAll('div', attrs = {'class': 'players'},limit=None)
row_lineup = 0
for lineup in lineups:
lineup1 = lineup.prettify()
lineup2 = lineup1.replace('>'&&'<',',')
df4 = pd.DataFrame(eval(lineup2))
df4.to_excel(writer, sheet_name='Starting Lineups', startrow=row_lineups, startcol=0)
row_lineups = row_lineups + len(df.index) + 3
writer.save()
我正在尝试从网页上获取首发阵容,将它们转换为pandas数据框,然后将其保存到excel文件中。我将问题转变为数据框时遇到了问题。我用逗号替换了括号,因为我认为这会把它变成csv格式。
答案 0 :(得分:0)
这可能会让你朝着正确的方向前进,每条线都是一条直线
data = [[x.text for x in y.findAll('a')] for y in lineups]
df = pd.DataFrame(data)