在python中将Web表导出到CSV

时间:2019-02-16 18:59:36

标签: python html-table export

我要在此网站中导出表格

https://www.timeanddate.com/moon/phases/usa/new-york?year=2000

到csv。

这是我用python写的代码:

import urllib.request


web = urllib.request.urlopen("https://www.timeanddate.com/moon/phases/usa/new-   york?year=2000")
s = web.read()

html = etree.HTML(s)



tr_nodes = html.xpath('//table[@id="Report1_dgReportDemographic"]/tr')


header = [i[0].text for i in tr_nodes[0].xpath("th")]


td_content = [[td.text for td in tr.xpath('td')] for tr in tr_nodes[1:]]


df = pd.DataFrame(np.array(td_content))


print(df)


df.to_csv('my data.csv')

但是我得到这个错误:

    header = [i[0].text for i in tr_nodes[0].xpath("th")]
IndexError: list index out of range

有人可以告诉我我在做什么错吗?

0 个答案:

没有答案