<p id="rrtt5"></p><ruby id="rrtt5"></ruby>

      系統城裝機大師 - 唯一官網:www.1999hs.com!

      當前位置:首頁 > 腳本中心 > 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 爬取疫情數據的源碼,希望對大家有所幫助!

      分享到:

      相關信息

      系統教程欄目

      欄目熱門教程

      人氣教程排行

      站長推薦

      熱門系統下載

      天堂资源中文WWW,久久精品女人天堂AV免费观看,无码专区一ⅤA亚洲V天堂,免费观看在线AⅤ天堂视频