老师布置的作业,字体文件的话可以去网上找[Python] 纯文本查看 复制代码 import json
import requests
import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import time
def screen_data():
url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
headers = {
"User-Agent": "Mozilla/6.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
}
province_list = requests.get(url, headers=headers, verify=True)
province_dict = json.loads(province_list.text)
province_dict_data = province_dict['data']
province_dicts= json.loads(province_dict_data)
province_dicts_data= province_dicts['areaTree'][0]['children']
today_time = time.strftime("%Y-%m-%d")
provinceList_data = []
for i in range(len(province_dicts_data)):
dict = {}
dict['name'] = province_dicts_data[i]['name']
dict['time'] = today_time
dict['today_confirm'] = province_dicts_data[i]['today']['confirm']
dict['total_confirm'] = province_dicts_data[i]['total']['confirm']
dict['total_heal'] = province_dicts_data[i]['total']['heal']
dict['total_dead'] = province_dicts_data[i]['total']['dead']
provinceList_data.append(dict)
return provinceList_data
def pd_excel():
provinceList_process = pd.DataFrame(list(screen_data()))
provinceList_data_dicts = {
'name': '省份',
'time': '统计时间',
'today_confirm': '今日新增',
'total_confirm': '累计新增',
'total_heal': '累计治愈',
'total_dead': '历史死亡'
}
provinceList_process.rename(columns = provinceList_data_dicts, inplace = True)
file_path = pd.ExcelWriter('./{}中国省级行政区疫情数据.xlsx'.format(time.strftime("%Y-%m-%d")))
provinceList_process.fillna(' ', inplace = True)
provinceList_process.to_excel(file_path, encoding = 'utf-8', index = False)
file_path.save()
def data_list():
provinceList_name_list = []
provinceList_today_confirm_list = []
for i in screen_data():
provinceList_name_list.append(i['name'])
provinceList_today_confirm_list.append(i['today_confirm'])
dic = dict(zip(provinceList_name_list,provinceList_today_confirm_list))
return dic
def wc_data():
wc_data = WordCloud(font_path = "./123.ttf",scale = 18,background_color = "white")
wc_data.generate_from_frequencies(data_list())
plt.imshow(wc_data)
plt.axis("off")
plt.show()
if __name__ == '__main__':
pd_excel()
wc_data()
|