python爬虫爬取疫情数据
时间: 2023-12-20 19:05:18 浏览: 187
使用python爬取疫情数据
5星 · 资源好评率100%
以下是使用Python爬虫爬取疫情数据的示例代码:
```python
import requests
import json
import pandas as pd
from pyecharts.charts import Map
from pyecharts import options as opts
# 发送网络请求,获取疫情数据
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
response = requests.get(url)
json_data = json.loads(response.text)
data = json.loads(json_data['data'])
# 数据处理
china_data = data['areaTree'][0]['children']
province_data = []
for item in china_data:
province_name = item['name']
province_total = item['total']['confirm']
province_today = item['today']['confirm']
province_dict = {'name': province_name, 'today': province_today, 'total': province_total}
province_data.append(province_dict)
# 数据可视化
df = pd.DataFrame(province_data)
df = df.sort_values(by='total', ascending=False)
province_list = list(df['name'])
total_list = list(df['total'])
map_data = [(province_list[i], total_list[i]) for i in range(len(province_list))]
map_chart = (
Map()
.add("累计确诊人数", map_data, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="全国疫情地图"),
visualmap_opts=opts.VisualMapOpts(max_=max(total_list)),
)
)
map_chart.render_notebook()
```
该代码使用requests库发送网络请求,获取疫情数据,并使用json库解析数据。然后使用pandas库对数据进行处理,提取出各个省份的累计确诊人数和今日新增确诊人数。最后使用pyecharts库将数据可视化为地图。
阅读全文