系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 脚本中心 > python > 详细页面

python 爬取疫情数据的源码

时间:2020-02-09来源:系统城作者:电脑系统城

疫情数据

python 爬取疫情数据的源码

程序源码


 
  1. // An highlighted block
  2. import requests
  3. import json
  4. class epidemic_data():
  5. def __init__(self, province):
  6. self.url = url
  7. self.header = header
  8. self.text = {}
  9. self.province = province
  10. # self.r=None
  11. def down_page(self):
  12. r = requests.get(url=url, headers=header)
  13. self.text = r.text
  14. # self.r = r
  15. def parse_page(self):
  16. # print(type(self.r.json()['data']))
  17. # 因为解析数据为 data 前有一个"",所以数据类型为string
  18. data_str = json.loads(self.text)['data'] #sring
  19. # print(type(data_dict))
  20. # print(type(data_dict['data']))
  21. # 将str 转化为对象
  22. data_json = json.loads(data_str)
  23. data_tree_dict = data_json['areaTree'][0]['children'] # 取中国的省列表
  24. prt_str = []
  25. prt_str.append("数据更新时间:"+data_json['lastUpdateTime'])
  26. prt_str.append("全国" + ":" + "累计确诊病例:" + str(data_json['chinaTotal']['confirm']) + \
  27. "累计疑似病例:" + str(data_json['chinaTotal']['suspect']) + \
  28. "累计死亡病例:" + str(data_json['chinaTotal']['dead']) + \
  29. "累计出院病例:" + str(data_json['chinaTotal']['heal']) + \
  30. "今日新增确诊病例:" + str(data_json['chinaAdd']['confirm']) + \
  31. "今日新增疑似病例:" + str(data_json['chinaAdd']['suspect']) + \
  32. "今日新增死亡病例:" + str(data_json['chinaAdd']['dead']) + \
  33. "今日新增出院病例:" + str(data_json['chinaAdd']['heal']))
  34. for province_list in data_tree_dict:
  35. for provice_name in self.province:
  36. if provice_name in province_list['name']:
  37. city_list = province_list['children']
  38. prt_str.append(province_list['name'] + ":" + "累计确诊病例:" + str(province_list['total']['confirm']) + \
  39. "累计死亡病例:" + str(province_list['total']['dead']) + \
  40. "累计出院病例:" + str(province_list['total']['heal']) + \
  41. "今日新增确诊病例:" + str(province_list['today']['confirm']) + \
  42. "今日新增死亡病例:" + str(province_list['today']['dead']) + \
  43. "今日新增出院病例:" + str(province_list['today']['heal']))
  44. if provice_name == '山东':
  45. for data_dict in city_list:
  46. prt_str.append(data_dict['name'] + ":" + "累计确诊病例:" + str(data_dict['total']['confirm']) +\
  47. "累计死亡病例:" + str(data_dict['total']['dead']) + \
  48. "累计出院病例:" + str(data_dict['total']['heal']) + \
  49. "今日确诊病例:" + str(data_dict['today']['confirm']) + \
  50. "今日死亡病例:" + str(data_dict['today']['dead']) + \
  51. "今日出院病例:" + str(data_dict['today']['heal']))
  52. for item in prt_str:
  53. print(item)
  54. a = data_tree_dict
  55. # print(type(data_tree_dict['chinaTotal']))
  56. # print(data_tree_dict.keys())
  57. def write_page(self):
  58. pass
  59. def show(self):
  60. pass
  61. def show(self):
  62. self.down_page()
  63. self.parse_page()
  64. if __name__ == '__main__':
  65. url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
  66. header = {
  67. 'user - agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
  68. }
  69. province = ['湖北','山东']
  70. wf = epidemic_data(province)
  71. wf.show()

总结

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

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载