美文网首页
Python爬取分析广东实时疫情数据

Python爬取分析广东实时疫情数据

作者: 刘小白DOER | 来源:发表于2021-06-27 14:52 被阅读0次

        最近看到利用Python分析全国的实时疫情数据来可视化展示,最近广东形势不好,笔者借用修改后来爬取广东的疫情数据。目标网站是腾讯新闻网实时数据,其原理主要是通过Requests获取Json请求,然后通过Matplotlib绘制图表。

        文末的代码托管在github上,laufei90/python: Some Python scripts (github.com) 。


    1、获取数据的请求url

        笔者使用的是edge浏览器,在页面右键-检查、网络-F5,得到url。

    2、通过分析url地址、请求方法、参数及响应格式,可以获取Json数据,注意url需要增加一个时间戳Unix timestamp。

    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)

    # 抓取腾讯疫情实时json数据

     data = json.loads(requests.get(url=url).json()['data'])

    3、数据数组处理获取广东的数据

    # 统计省份信息

    num = data['areaTree'][0]['children']

    # 广东省总数据

    gdong = num[1]

    查看返回的gdong的json数据:

    {'nowConfirm': 168, 'confirm': 2728, 'suspect': 0, 'dead': 8, 'deadRate': '0.29', 'showRate': False, 'heal': 2552, 'healRate': '93.55', 'showHeal': True, 'wzz': 15}

    nowConfirm是现存确诊,confirm是总确诊,suspect是疑似,。。。

    4、数据数组处理获取广东各区域的现存确诊数据

    gdong_children_total_data = {}

    for item in gdong['children']:

        if item['name'] not in gdong_children_total_data:

            gdong_children_total_data.update({item['name']:0})

        gdong_children_total_data[item['name']] += int(item['total']['nowConfirm']) 

    print(gdong_children_total_data)

    5、Matplotlib绘制广东各地区柱状图

    plt.rcParams['font.sans-serif'] = ['simhei']   

    plt.figure(figsize=[10,6])

    plt.bar(gd_names,gd_numbers,color='green')

    plt.xlabel("地区", size=12)

    plt.ylabel("确诊人数", fontproperties='SimHei', rotation=90, size=12)

    plt.title("广东省不同地区疫情现存确诊数对比图", size=16)

    plt.xticks(list(gd_names), rotation=90, size=12)    

    plt.show()

    6、实际运行效果

    测试源代码展示:

    相关文章

      网友评论

          本文标题:Python爬取分析广东实时疫情数据

          本文链接:https://www.haomeiwen.com/subject/rkkxultx.html