我要在此网站中导出表格
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
有人可以告诉我我在做什么错吗?