写了一个江西省的疫情地图,爬的是网易的数据,加上pyecharts
效果图是这样的,你们使用的时候只需将省江西这两个字改成你们想要的省份就行。
话不多说,上代码(headers弄得有点多,你们随意)
# -*- coding: utf-8 -*-
import re
import time
from pyecharts.charts import Map
from pyecharts import options as opts
import requests
import json
import random
User_Agent_List = [
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"]
User_Agent = random.choice(User_Agent_List)
headers = {"User-Agent": User_Agent}
url = "https://c.m.163.com/ug/api/wuhan/app/index/feiyan-data-list?t=1580469818264"
#爬取疫情数据
def geturl(url):
try:
response = requests.get(url,headers=headers)
print(response.status_code)
if response.status_code == 200:
content_field = json.loads(response.text)
list_datas_1 = content_field['data']['list']
return list_datas_1
else:
print('返回代码:'+response.status_code)
return None
except Exception as e:
print('此页有问题!',e)
return None
#制作疫情地图
def makemap(dict):
# 省和直辖市
province_distribution = dict
value=province_distribution.values()
# maptype='china' 只显示全国直辖市和省级
title = str(int(time.strftime("%Y%m%d"))-1)+"江西疫情地图"
map = Map()
map.set_global_opts(
title_opts=opts.TitleOpts(title=title),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True,
pieces=[
{"min": 191, "label": ">190", "color": "#780707"}, #数据范围分段,分颜色,可以根据数据大小具体分配大小
{"max": 190, "min": 151, "label": "190-151", "color": "#8A0808"},
{"max": 150, "min": 121, "label": "150-121", "color": "#B40404"},
{"max": 120, "min": 91, "label": "120-91", "color": "#CD1111"},
{"max": 90, "min": 61, "label": "30-21", "color": "#DF0101"},
{"max": 60, "min": 31, "label": "60-31", "color": "#F68181"},
{"max": 30, "min": 1, "label": "30-1", "color": "#F5A9A9"},
{"max": 0, "min": 0, "label": "0", "color": "#FFFFFF"},
], ) #最大数据范围,分段
)
map.add(title, data_pair=province_distribution.items(), maptype="江西", is_roam=True)
map.render('江西疫情地图.html')
#生成本省疫情列表
def makedict(list):
dict1 = {}
for item in list:
for k,v in item.items():
if( v == "江西"): #替换“江西”,可以查各省的数据
dict1[item['name']+'市'] = int(item["confirm"])
return dict1
if __name__ == '__main__':
list_data = geturl(url)
dict_data = makedict(list_data)
makemap(dict_data)