刮盒用美体素,用Pandas出口到外销

2024-09-29 00:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在试图找出如何用python3.6和beauthoulsoup和Pandas模块从Fangraphs获取棒球箱得分。我的最终目标是在Excel中将网页的不同部分保存到不同的工作表中。在

为了做到这一点,我想我必须通过每个表各自的id标签来分别拉动它们。这是为组成第一个excel表的四个表(在页面上的图形下方)执行此操作的代码。运行代码会导致以下错误:

Traceback (most recent call last):

File "Fangraphs Box Score Scraper.py", line 14, in <module>
df1 = pd.read_html(soup,attrs={'id': ['WinsBox1_dghb','WinsBox1_dghp','WinsBox1_dgab','WinsBox1_dgap']})

File "C:\Python36\lib\site-packages\pandas\io\html.py", line 906, in read_html
keep_default_na=keep_default_na)

File "C:\Python36\lib\site-packages\pandas\io\html.py", line 743, in _parse
raise_with_traceback(retained)

File "C:\Python36\lib\site-packages\pandas\compat\__init__.py", line 344, in raise_with_traceback

raise exc.with_traceback(traceback)

TypeError: 'NoneType' object is not callable

^{pr2}$

Tags: inpypandaslibpackageshtmlwithline
1条回答
网友
1楼 · 发布于 2024-09-29 00:23:18

你用错了id,你采用了<div>的形式,但需要从<table>标签read_html attrs开始,我想你不需要使用bs,试试看:

import pandas as pd

url = 'http://www.fangraphs.com/boxscore.aspx?date=2017-09-10&team=Red%20Sox&dh=0&season=2017'
df1 = pd.read_html(
    url,
    attrs={'id': ['WinsBox1_dghb_ctl00', 'WinsBox1_dgab_ctl00']}
)

# and now df1 it is list of df
writer = pd.ExcelWriter('Box Scores.xlsx')
row = 0
for df in df1:
    df.to_excel(writer, sheet_name='tables', startrow=row , startcol=0)   
    row = row + len(df.index) + 3

writer.save()

相关问题 更多 >