import requests # 爬取网页
import json # 爬取数据
import xlwings as xw # 导入excel
url = 'https://file1.dxycdn.com/2022/1119/972/1578037167536673853-135.json?t=27813969'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
response = requests.get(url, headers=headers)
wb = xw.Book() # 相当于打开excel操作
sht = wb.sheets['sheet1'] # 相当于在excel里加了一个工作表
sht.range('A1').value = ['累计确诊']
sht.range('B1').value = ['今日新增']
sht.range('C1').value = ['累计死亡']
sht.range('D1').value = ['累计治愈']
sht.range('E1').value = ['日期']
json_data = response.json()['data']
# print(json_data)
print('累计确诊', '今日新增', '累计死亡', '累计治愈', '日期')
for i in range(709, 1031):
earth_data = json_data[i]
# print(earth_data)
# name = earth_data['name']
# sht.range(f'A{i-707}').value = name
confirmedCount = json.dumps(earth_data['confirmedCount'])
sht.range(f'A{i - 707}').value = confirmedCount
# print(confirmedCount)
confirmedIncr = json.dumps(earth_data['confirmedIncr'])
sht.range(f'B{i - 707}').value = confirmedIncr
deadCount = json.dumps(earth_data['deadCount'])
sht.range(f'C{i - 707}').value = deadCount
curedCount = json.dumps(earth_data['curedCount'])
sht.range(f'D{i - 707}').value = curedCount
date = earth_data['dateId']
date_ = str(date)[0:4] + '/' + str(date)[4:6] + '/' + str(date)[6:8]
sht.range(f'E{i - 707}').value = date_
print(confirmedCount, confirmedIncr, deadCount, curedCount, date_)
wb.save('中国疫情数据.xlsx')
中国疫情数据爬取
最新推荐文章于 2023-05-16 15:26:50 发布