Using Python to make novel coronavirus outbreaks in real-time map

Has recently been engaged in a new type of coronavirus panic, many cities are infected with the virus, today small to share with Python make novel coronavirus outbreaks in real-time map, and interested friends to follow Xiaobian look at it
up first every morning last week thing is to open the software to see news related to the outbreak news. The number of under diagnosis and understanding of their own relatives and friends in your city, but pure digital still lack an intuitive concept. Then we do a bar.

As for data, real-time epidemic pages from the major sites can get. Taking a site, for example, get the html with requests, and found no data. Do not panic, it proved to be a javascript rendering of the page, even javascript is required to take data from the background. Open Chrome Developer Tools, opening the network, refresh the page, click on each request, there must be a json is to take a
Here Insert Picture Description
note here of return data is contained in a js variable, then the next treated with n-like, and comes with python json.loads method can be converted to the dict

result = requests.get(
   'https://interface.sina.cn/news/wap/fymap2020_data.d.json?1580097300739&&callback=sinajp_1580097300873005379567841634181')
 json_str = re.search("\(+([^)]*)\)+", result.text).group(1)
 
 html = f"{json_str}"
 table = json.loads(f"{html}")

Very simple data format, data and data provincial subordinate cities

{ 'city': [  {  'conNum': '4',
         'cureNum': '0',
         'deathNum': '0',
         'name': '乌鲁木齐',
         'susNum': '0'},
       {  'conNum': '1',
         'cureNum': '0',
         'deathNum': '0',
         'name': '伊犁州',
         'susNum': '0'}],
 'cureNum': '0',
 'deathNum': '0',
 'name': '新疆',
 'susNum': '0',
 'value': '5'}

Good English students can directly cure guess cureNum number, deathNum is the number of deaths. value should be confirmed several other fields can be determined based on the original site form

We recommend learning Python buckle qun: 774711191, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering
the most intuitive chart a course map. Domestic manufacturers out of the country map echarts naturally have to have the best support. Package echarts-china-provinces-pypkg with pip installation pyecharts, and two data
echarts-china-cities-pypkg to

Under json traversal data, confirmed the existence of a province and the number of data in the data passed to map objects like

for province in table['data']['list']:
    pp.pprint(province)
    data.append((province['name'], province['value']))
 
    for city in province['city']:
      pp.pprint(city)
map_p = Map()
map_p.set_global_opts(title_opts=opts.TitleOpts(title="实时疫情图"), visualmap_opts=opts.VisualMapOpts(max_=100))
map_p.add("确诊", data, maptype="china")
map_p.render("ncov.html") # 生成html文件

The code is simple, pay attention to the decision rendered colors max. FIG effect as
Here Insert Picture Description
100 provinces confirmed at a glance. Interested students can also make the city level or world map.

Finally, I wish it as soon as possible will turn blue. Wuhan Come on, Come on, China!

to sum up

The above is a small series to introduce the use Python to make novel coronavirus outbreaks in real-time map

Published 37 original articles · won praise 41 · views 40000 +

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104254936