import requests
import json
from pyecharts import Map
from lxml import etree
class S:
def __init__(self):
self.headers={
"User-Agent" :"Mozilla / 5.0(iPhone;CPUiPhoneOS11_0likeMacOSX) AppleWebKit / 604.1.38(KHTML, likeGecko) Version / 11.0Mobile / 15A372Safari / 604.1"
}
self.url = "https://ncov.dxy.cn/ncovh5/view/pneumonia_peopleapp"
def parse_url(self):
r = requests.get(url=self.url,headers=self.headers)
assert r.status_code==200
html = etree.HTML(r.content.decode( ))
results=html.xpath('//*[@id="getListByCountryTypeService1"]//text()')[0].split('}')[:-3]
return results
def getDaraList(self,results):
data_list = []
for result in results:
data_dict={}
if results.index(result)==0:
result = result.replace('try { window.getListByCountryTypeService1 = [','')
result =json.loads(result.lstrip(',')+'}')
data_dict["provinceShortName"]=result["provinceShortName"]
data_dict["confirmedCount"]=result["confirmedCount"]
data_dict["deadCount"]=result["deadCount"]
data_dict["curedCount"]=result["curedCount"]
data_list.append(data_dict)
return data_list
return data_list
def main(self):
results = self.parse_url()
data_list = self.getDaraList(results)
return data_list
S=S()
data_list = S.main()
provinceShortName_list=[]
confirmedCount_list=[]
deadCount_list=[]
curedCount_list=[]
for i in data_list:
provinceShortName_list.append(i['provinceShortName'])
confirmedCount_list.append(i['confirmedCount'])
deadCount_list.append(i["deadCount"])
curedCount_list.append(i["curedCount"])
map = Map("中国疫情分布图",'',width=1980,height=1024,title_pos="center")
map.add("",provinceShortName_list,confirmedCount_list,visual_range=[0, 1000],maptype="china",is_visualmap=True,
visual_text_color="#000" , is_label_show=True)
map.show_config()
map.render(path="./中国疫情图.html")
Python爬虫——疫情地图
最新推荐文章于 2023-04-19 09:06:45 发布