我正在尝试解析一个网站的表格,而且我还很吵。对于每个链接,仅将第二个表/数据帧附加到SS。有多个链接,因此需要一个while循环。使用我能找到的小东西只是我只是坚持这个,我很确定完全关闭:
import pandas as pd
from pandas import ExcelWriter
a=1
alist = []
writer = ExcelWriter('name.xlsx')
def dffunc():
dfs = pd.read_html('http://websitepath{}.htm'.format(a))
df = dfs[1]
alist.append(df,ignore_index=True)
alist = pd.concat(df, axis=0)
while a<9:
dffunc()
a+=1
alist.to_excel(writer, index=False)
writer.save()
答案 0 :(得分:0)
df = dfs [1]获取列表中的第二个表。这就是你想要的吗?
答案 1 :(得分:0)
旧:
df = dfs[1]
alist.append(df,ignore_index=True)
alist = pd.concat(df, axis=0)
新:
import pandas as pd
from pandas import ExcelWriter
writer = ExcelWriter('name.xlsx')
writer_kwargs = {'index': False}
A = 9
def dffunc(a):
dfs = pd.read_html('http://websitepath{}.htm'.format(a))
return pd.concat(dfs, axis=0)
def dfhandler(df, writer, **kwargs):
df.to_excel(writer, sheet_name=a, **kwargs)
for a in xrange(1, A):
dfhandler(dffunc(a), writer, **writer_kwargs)
writer.save()