吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2359|回复: 2
收起左侧

[Python 转载] 中国省级行政区疫情数据存储和词云生成

[复制链接]
nianboy 发表于 2022-3-23 15:18
老师布置的作业,字体文件的话可以去网上找
[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()













发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

8204118 发表于 2022-3-23 18:38
我都不做作业
 楼主| nianboy 发表于 2022-3-23 22:41
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表