python 爬取疫情数据的源码
作者:cynewwalk 时间:2022-05-22 13:21:54
疫情数据
程序源码
// 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?name=disease_h5'
header = {
'user - agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
province = ['湖北','山东']
wf = epidemic_data(province)
wf.show()
总结
以上所述是小编给大家介绍的python 爬取疫情数据的源码,希望对大家有所帮助!
来源:https://blog.csdn.net/cynewwalk/article/details/104214740
标签:python,疫情,数据
0
投稿
猜你喜欢
用python实现的线程池实例代码
2023-11-06 23:04:54
对有自增长字段的表导入数据注意事项
2024-01-22 17:54:51
MSSQL 2000 使用帮助(sql server简明教程)
2024-01-22 06:17:34
vue通信方式EventBus的实现代码详解
2024-05-10 14:16:38
Python统计文件中去重后uuid个数的方法
2023-07-11 17:20:06
python常用时间库time、datetime与时间格式之间的转换教程
2023-05-03 01:24:37
Scrapy 配置动态代理IP的实现
2023-12-10 19:06:22
Jquery对数组的操作技巧整理
2024-04-22 22:32:52
MySQL 选择合适的存储引擎
2024-01-21 08:55:24
golang 用msgpack高效序列化的案例
2024-04-26 17:32:52
用Python脚本生成Android SALT扰码的方法
2023-05-09 22:12:25
解决Keyerror ''acc'' KeyError: ''val_acc''问题
2022-09-05 11:28:12
pandas如何删除没有列名的列浅析
2021-05-17 11:08:47
CentOS 7 安装 MySQL 5.6遇到的各种问题小结
2024-01-20 23:51:39
深入理解Python中的内置常量
2023-01-21 02:57:47
Java使用JDBC向MySQL数据库批次插入10W条数据(测试效率)
2024-01-15 15:41:11
认识延迟时间为 0 的 setTimeout
2008-04-04 16:37:00
GoLang中生成UUID唯一标识的实现
2024-05-22 10:19:54
总结Python编程中三条常用的技巧
2022-06-04 05:52:04
CentOS 6.5下安装Python 3.5.2(与Python2并存)
2021-06-24 04:33:54