如何使用Python读取WebPage并写入平面文件?

时间:2017-11-10 21:07:30

标签: python

非常适合Python的新手。 尝试阅读此页面上显示的表格(当前过滤器按原样设置),然后将其写入csv文件。

http://www65.myfantasyleague.com/2017/options?L=47579&O=243&TEAM=DAL&POS=RB

我尝试了下一种方法。它创建了csv文件,但没有用实际的表内容填充它。

提前感谢任何帮助。感谢。

import requests
import pandas as pd

url = 'http://www65.myfantasyleague.com/2017/optionsL=47579&O=243&TEAM=DAL&POS=RB'
csv_file='DAL.RB.csv'
pd.read_html(requests.get(url).content)[-1].to_csv(csv_file)

1 个答案:

答案 0 :(得分:0)

通常,尝试更好地强调您的问题,尝试调试并且不要将所有内容放在一行中。话虽如此,您的具体问题是代码中的索引和缺少的?options之后):

import requests
import pandas as pd

url = 'http://www65.myfantasyleague.com/2017/options?L=47579&O=243&TEAM=DAL&POS=RB'
#                                                  -^-
csv_file='DAL.RB.csv'

pd.read_html(requests.get(url).content)[1].to_csv(csv_file)
#                                      -^-

这将生成一个CSV文件,其中包含表格。