您的位置:首页 > 技术中心 > 其他 >

Python实时数据采集-新型冠状病毒

时间:2022-02-24 10:24


Python实时数据采集-新型冠状病毒

源代码 来源:https://github.com/Programming-With-Love/2019-nCoV

疫情数据时间为:2020.2.1

项目相关截图:

全国数据展示

国内数据展示

国外数据展示

查看指定区域详细数据

源代码,注意安装所需模块(例如 pip install 模块名)

  1. import requests
  2. import re
  3. from bs4 import BeautifulSoup
  4. from time import sleep
  5. import json
  6. from prettytable import ALL
  7. from prettytable import PrettyTable
  8. hubei = {}
  9. guangdong = {}
  10. zhejiang = {}
  11. beijing = {}
  12. shanghai = {}
  13. hunan = {}
  14. anhui = {}
  15. chongqing = {}
  16. sichuan = {}
  17. shandong = {}
  18. guangxi = {}
  19. fujian = {}
  20. jiangsu = {}
  21. henan = {}
  22. hainan = {}
  23. tianjin = {}
  24. jiangxi = {}
  25. shanxi1 = {} # 陕西
  26. guizhou = {}
  27. liaoning = {}
  28. xianggang = {}
  29. heilongjiang = {}
  30. aomen = {}
  31. xinjiang = {}
  32. gansu = {}
  33. yunnan = {}
  34. taiwan = {}
  35. shanxi2 = {} # 山西
  36. jilin = {}
  37. hebei = {}
  38. ningxia = {}
  39. neimenggu = {}
  40. qinghai = {} # none
  41. xizang = {} # none
  42. provinces_idx = [hubei, guangdong, zhejiang, chongqing, hunan, anhui, beijing,
  43. shanghai, henan, guangxi, shandong, jiangxi, jiangsu, sichuan,
  44. liaoning, fujian, heilongjiang, hainan, tianjin, hebei, shanxi2,
  45. yunnan, xianggang, shanxi1, guizhou, jilin, gansu, taiwan,
  46. xinjiang, ningxia, aomen, neimenggu, qinghai, xizang]
  47. map = {
  48. '湖北':0, '广东':1, '浙江':2, '北京':3, '上海':4, '湖南':5, '安徽':6, '重庆':7,
  49. '四川':8, '山东':9, '广西':10, '福建':11, '江苏':12, '河南':13, '海南':14,
  50. '天津':15, '江西':16, '陕西':17, '贵州':18, '辽宁':19, '香港':20, '黑龙江':21,
  51. '澳门':22, '新疆':23, '甘肃':24, '云南':25, '台湾':26, '山西':27, '吉林':28,
  52. '河北':29, '宁夏':30, '内蒙古':31, '青海':32, '西藏':33
  53. }
  54. def getTime(text):
  55. TitleTime = str(text)
  56. TitleTime = re.findall('<span>(.*?)</span>', TitleTime)
  57. return TitleTime[0]
  58. def getAllCountry(text):
  59. AllCountry = str(text)
  60. AllCountry = AllCountry.replace("[<p class=\"confirmedNumber___3WrF5\"><span class=\"content___2hIPS\">", "")
  61. AllCountry = AllCountry.replace("<span style=\"color: #4169e2\">", "")
  62. AllCountry = re.sub("</span>", "", AllCountry)
  63. AllCountry = AllCountry.replace("</p>]", "")
  64. AllCountry = AllCountry.replace("<span style=\"color: rgb(65, 105, 226);\">", "")
  65. AllCountry = re.sub("<span>", "", AllCountry)
  66. AllCountry = re.sub("<p>", "", AllCountry)
  67. AllCountry = re.sub("</p>", "", AllCountry)
  68. return AllCountry
  69. def query(province):
  70. table = PrettyTable(['地区', '确诊', '死亡', '治愈'])
  71. for (k, v) in province.items():
  72. name = k
  73. table.add_row([name, v[0] if v[0] != 0 else '-', v[1] if v[1] != 0 else '-', v[2] if v[2] != 0 else '-'])
  74. if len(province.keys()) != 0:
  75. print(table)
  76. else:
  77. print("暂无")
  78. def getInfo(text):
  79. text = str(text)
  80. text = re.sub("<p class=\"descText___Ui3tV\">", "", text)
  81. text = re.sub("</p>", "", text)
  82. return text
  83. def is_json(json_str):
  84. try:
  85. json.loads(json_str)
  86. except ValueError:
  87. return False
  88. return True
  89. def ff(str, num):
  90. return str[:num] + str[num+1:]
  91. def main():
  92. url = "https://3g.dxy.cn/newh5/view/pneumonia"
  93. try:
  94. headers = {}
  95. headers['user-agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36' #http头大小写不敏感
  96. headers['accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
  97. headers['Connection'] = 'keep-alive'
  98. headers['Upgrade-Insecure-Requests'] = '1'
  99. r = requests.get(url, headers=headers)
  100. r.raise_for_status()
  101. r.encoding = r.apparent_encoding
  102. soup = BeautifulSoup(r.text,'lxml')
  103. table = PrettyTable(['地区', '确诊', '死亡', '治愈'])
  104. table.hrules = ALL
  105. #### 截至时间
  106. # TitleTime = getTime(soup.select('.title___2d1_B'))
  107. print()
  108. # print(" ",TitleTime + "\n")
  109. while True:
  110. r = requests.get("https://service-f9fjwngp-1252021671.bj.apigw.tencentcs.com/release/pneumonia")
  111. json_str = json.loads(r.text)
  112. if json_str['error'] == 0:
  113. break
  114. print("==================================全国数据==================================")
  115. print()
  116. print(" 确诊 " + str(json_str['data']['statistics']['confirmedCount']) + " 例"
  117. + " " + "疑似 " + str(json_str['data']['statistics']['suspectedCount']) + " 例"
  118. + " " + "死亡" + str(json_str['data']['statistics']['deadCount']) + " 例"
  119. + " " + "治愈" + str(json_str['data']['statistics']['curedCount']) + " 例\n")
  120. print("==================================相关情况==================================")
  121. print()
  122. print("传染源:" + json_str['data']['statistics']['infectSource'])
  123. print("病毒:" + json_str['data']['statistics']['virus'])
  124. print("传播途径:" + json_str['data']['statistics']['passWay'])
  125. print(json_str['data']['statistics']['remark1'])
  126. print(json_str['data']['statistics']['remark2'] + "\n")
  127. print("==================================国内情况==================================")
  128. print()
  129. json_provinces = re.findall("{\"provinceName\":(.*?)]}", str(soup))
  130. idx = 0
  131. for province in json_provinces:
  132. if is_json(province):
  133. pass
  134. else:
  135. province = "{\"provinceName\":" + province + "]}"
  136. province = json.loads(province)
  137. province_name = province['provinceShortName'] if province['provinceShortName'] != 0 else '-'
  138. confirmed = province['confirmedCount'] if province['confirmedCount'] != 0 else '-'
  139. suspected = province['suspectedCount'] if province['suspectedCount'] != 0 else '-'
  140. cured = province['curedCount'] if province['curedCount'] != 0 else '-'
  141. dead = province['deadCount'] if province['deadCount'] != 0 else '-'
  142. table.add_row([province_name, confirmed, dead, cured])
  143. map[province_name] = idx
  144. idx = idx + 1
  145. for city in province['cities']:
  146. provinces_idx[map[province_name]][city['cityName']] = [city['confirmedCount'], city['deadCount'], city['curedCount']]
  147. print(table)
  148. print()
  149. print("==================================国外情况==================================")
  150. print()
  151. json_provinces = str(re.findall("\"id\":949(.*?)]}", str(soup)))
  152. json_provinces = json_provinces[:1] + "{\"id\":949" + json_provinces[2:]
  153. json_provinces = json_provinces[:len(json_provinces) - 2] + json_provinces[len(json_provinces) - 1:]
  154. provinces = json.loads(json_provinces)
  155. table = PrettyTable(['地区', '确诊', '死亡', '治愈'])
  156. for province in provinces:
  157. confirmed = province['confirmedCount'] if province['confirmedCount'] != 0 else '-'
  158. dead = province['deadCount'] if province['deadCount'] != 0 else '-'
  159. cured = province['curedCount'] if province['curedCount'] != 0 else '-'
  160. table.add_row([province['provinceName'], confirmed, dead, cured])
  161. print(table)
  162. print()
  163. print("==================================最新消息==================================")
  164. print()
  165. idx = 0
  166. for news in json_str['data']['timeline']:
  167. if idx == 5:
  168. break
  169. print(news['pubDateStr'] + " " + news['title'])
  170. idx = idx + 1
  171. print()
  172. key = input("请输入您想查询详细信息的省份,例如 湖北\n")
  173. print()
  174. if key in map.keys():
  175. query(provinces_idx[map[key]])
  176. else:
  177. print("暂无相关信息")
  178. print("\n欢迎提出各种意见")
  179. except:
  180. print("连接失败")
  181. if __name__ == '__main__':
  182. main()
  183. sleep(30)

最后,祝大家百毒不侵,中国加油!!一定能够度过难关!!

以上就是Python实时数据采集-新型冠状病毒的详细内容,更多请关注gxlsystem.com其它相关文章!

热门排行

今日推荐

热门手游