失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 新型冠状病毒肺炎分析

新型冠状病毒肺炎分析

时间:2018-09-04 06:27:28

相关推荐

新型冠状病毒肺炎分析

的春节,因为全国性的爆发新型冠状病毒肺炎,疫情严重,为了不给国家添麻烦,正月初二从老家返回后一直宅在家里。每天关注疫情的进展,就对疫情做了一次数据分析。数据从网易的肺炎疫情实时动态播报获取(/special/epidemic/)。本次分析中使用了如下技术:

爬虫 ——获取肺炎疫情数据

数据可视化 ——数据分析

机器学习 ——实时预测

应用构建 ——展示数据

因为,Python2从开始停止支持,因此,在本次分析中使用Python3进行。

获取肺炎疫情数据

每天国家以及各省市的卫健委都会通报疫情的实时数据,各大门户网站都会从卫健委获取数据,实时通报,在这里选择从网易获取数据。数据获取使用Python的类库requests-html。首先,使用如下命令安装类库:

pip install requests-html

类库安装成功后,首先使用requests-html从网易获取数据,并使用整理数据。代码如下:

from requests_html import HTMLSessionwith HTMLSession() as session:r = session.get('/special/epidemic/')r.html.render() # 首次使用,自动下载chromiumhtml = r.html.htmltarget = html.split('<script>')[3]target = target.split('</script>')[0]dataset = target.split(" = ")details = dataset[1].replace('window.data_by_date', '').replace('\n', '').replace(' ', '')details = details.replace('name', '"name"')details = details.replace('province', '"province"')details = details.replace('confirm', '"confirm"')details = details.replace('suspect', '"suspect"')details = details.replace('heal', '"heal"')details = details.replace('dead', '"dead"')details = eval(details)# print(details)data_by_date = dataset[2].replace(';', '').replace('\n', '').replace(' ', '').replace('\t', '')# print(data_by_date)data_by_date = data_by_date.replace('date:"', '"date":".')data_by_date = data_by_date.replace('confirm', '"confirm"')data_by_date = data_by_date.replace('suspect', '"suspect"')data_by_date = data_by_date.replace('heal', '"heal"')data_by_date = data_by_date.replace('dead', '"dead"')data_by_date = data_by_date.replace('"confirm"_added', '"confirm_added"')data_by_date = data_by_date.replace('.', '-')data_by_date = eval(data_by_date)for i in range(1, len(data_by_date)):data = data_by_date[i]data['confirm_added'] = data['confirm'] - data_by_date[i -1]['confirm']for data in data_by_date:print(data)

在1月30的调试结果如下:

{'date': '-01-20', 'confirm': 291, 'suspect': 0, 'heal': 0, 'dead': 6, 'confirm_added': 93}{'date': '-01-21', 'confirm': 440, 'suspect': 0, 'heal': 0, 'dead': 9, 'confirm_added': 149}{'date': '-01-22', 'confirm': 571, 'suspect': 393, 'heal': 28, 'dead': 17, 'confirm_added': 131}{'date': '-01-23', 'confirm': 830, 'suspect': 1072, 'heal': 34, 'dead': 25, 'confirm_added': 259}{'date': '-01-24', 'confirm': 1287, 'suspect': 1965, 'heal': 38, 'dead': 41, 'confirm_added': 457}{'date': '-01-25', 'confirm': 1975, 'suspect': 2684, 'heal': 49, 'dead': 56, 'confirm_added': 688}{'date': '-01-26', 'confirm': 2744, 'suspect': 5794, 'heal': 51, 'dead': 80, 'confirm_added': 769}{'date': '-01-27', 'confirm': 4515, 'suspect': 6973, 'heal': 60, 'dead': 106, 'confirm_added': 1771}{'date': '-01-28', 'confirm': 5974, 'suspect': 9239, 'heal': 103, 'dead': 132, 'confirm_added': 1459}{'date': '-01-29', 'confirm': 7711, 'suspect': 12167, 'heal': 124, 'dead': 170, 'confirm_added': 1737}

接下来将会把数据保存到数据库,留待以后分析时使用。数据量相对较小,使用sqlite3来保存数据。Python3中内置了sqlite,可以直接导入sqlite3来创建,管理数据库。具体的数据库的操作,下次更新。

如果觉得《新型冠状病毒肺炎分析》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。