我想从以下链接中提取表https://www.imei.info/carriers/并将其保存到csv以下是我的代码:
for i in range(1,44):
url = 'https://www.imei.info/carriers/?page='+str(i)
html = requests.get(url).content
df_list = pd.read_html(html)
df = df_list[-1]
df.to_csv('imei.csv')
在csv中读取时,它只显示最后一页的数据
这是因为每次写入to_csv
时都会擦除上一页。您可以做的是检索所有DataFrames,concat
DataFrames列表,然后将输出写入csv:
dfs = []
for i in range(1,44):
url = 'https://www.imei.info/carriers/?page='+str(i)
html = requests.get(url).content
df_list = pd.read_html(html)
dfs.append(df_list[-1])
output = pd.concat(dfs)
output.to_csv("imei.csv")