脚本专栏 
首页 > 脚本专栏 > 浏览文章

python 爬取疫情数据的源码

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

疫情数据

python 爬取疫情数据的源码

程序源码

// An highlighted block
import requests
import json
class epidemic_data():
  def __init__(self, province):
    self.url = url
    self.header = header
    self.text = {}
    self.province = province
    # self.r=None
  def down_page(self):
    r = requests.get(url=url, headers=header)
    self.text = r.text
    # self.r = r
  def parse_page(self):
    # print(type(self.r.json()['data']))
    # 因为解析数据为 data 前有一个"",所以数据类型为string
    data_str = json.loads(self.text)['data'] #sring
    # print(type(data_dict))
    # print(type(data_dict['data']))
    # 将str 转化为对象
    data_json = json.loads(data_str)
    data_tree_dict = data_json['areaTree'][0]['children'] # 取中国的省列表
    prt_str = []
    prt_str.append("数据更新时间:"+data_json['lastUpdateTime'])
    prt_str.append("全国" + ":" + "累计确诊病例:" + str(data_json['chinaTotal']['confirm']) +             "累计疑似病例:" + str(data_json['chinaTotal']['suspect']) +             "累计死亡病例:" + str(data_json['chinaTotal']['dead']) +             "累计出院病例:" + str(data_json['chinaTotal']['heal']) +             "今日新增确诊病例:" + str(data_json['chinaAdd']['confirm']) +             "今日新增疑似病例:" + str(data_json['chinaAdd']['suspect']) +             "今日新增死亡病例:" + str(data_json['chinaAdd']['dead']) +             "今日新增出院病例:" + str(data_json['chinaAdd']['heal']))
    for province_list in data_tree_dict:
      for provice_name in self.province:
        if provice_name in province_list['name']:
          city_list = province_list['children']
          prt_str.append(province_list['name'] + ":" + "累计确诊病例:" + str(province_list['total']['confirm']) +                     "累计死亡病例:" + str(province_list['total']['dead']) +                     "累计出院病例:" + str(province_list['total']['heal']) +                     "今日新增确诊病例:" + str(province_list['today']['confirm']) +                     "今日新增死亡病例:" + str(province_list['today']['dead']) +                     "今日新增出院病例:" + str(province_list['today']['heal']))
          if provice_name == '山东':
            for data_dict in city_list:
              prt_str.append(data_dict['name'] + ":" + "累计确诊病例:" + str(data_dict['total']['confirm']) +                       "累计死亡病例:" + str(data_dict['total']['dead']) +                       "累计出院病例:" + str(data_dict['total']['heal']) +                       "今日确诊病例:" + str(data_dict['today']['confirm']) +                       "今日死亡病例:" + str(data_dict['today']['dead']) +                       "今日出院病例:" + str(data_dict['today']['heal']))
    for item in prt_str:
      print(item)
    a = data_tree_dict
    # print(type(data_tree_dict['chinaTotal']))
    # print(data_tree_dict.keys())
  def write_page(self):
    pass
  def show(self):
    pass
  def show(self):
    self.down_page()
    self.parse_page()
if __name__ == '__main__':
  url = 'https://view.inews.qq.com/g2/getOnsInfo"color: #ff0000">总结

以上所述是小编给大家介绍的python 爬取疫情数据的源码,希望对大家有所帮助!

上一篇:Python列表操作方法详解
下一篇:Python2与Python3的区别详解
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap