The novel Coronavirus pandemic of the last two months will mark the memory of every Chinese. Until now, the epidemic situation is still very serious. Although the number of confirmed cases in other provinces except Hubei has decreased for 11 consecutive days, there are still at least 160 million people on the move, which is undoubtedly still a certain risk.

Believe that a lot of people and I am, in the morning the first thing is to check the epidemic data, the data are usually equipped with a map of China the number of confirmed, see CSDN pushed before an article, there would be students with a python implementation with the map, I feel very interesting, he also try to write one, mainly use the following library:

- Network requests: Requests - Charting library: PyechartsCopy the code

Access to epidemic data

The epidemic data can be found in both Tencent News and Alipay, but it is relatively difficult to obtain the data of Alipay, so I decided to start with the data of Tencent News. First, open the link in Chrome: https://news.qq.com//zt2020/page/feiyan.htm, and then through the chrome developer tools (F12) filter corresponding to the XHR interface (XHR format interfaces, is ajax asynchronous request network data interface, general interfaces defined by XHR filtering), The diagram below:

By viewing all the interfaces in the figure, we can obtain the number of hospitals in each province and query the daily number of newly confirmed cases according to the name of each province, but we do not have the data of the number of confirmed cases in each province. Let’s switch to the Chrome Developer Tools console to see if the developer has printed the corresponding data. Bingo! Sure enough, I found some useful information:

Let’s look at the two key pieces of information, one is the URL in the red box, and the other is the whole printed data, from which we find the total number of diagnoses we need for each province in the country. With the data source identified, use the Requests library to request the data and extract the data from the children below areaTree above:

Get (data_url).json()[“data”] returns a string that needs to be converted to JSON in order to extract the value from it. Now that we have the data, we’re all set. Next we started to map China.

map

There are two main graphing libraries in Python: Matplotlib and Pyecharts.

  • Matplotlib provides BaseMap that can be used to draw maps, but I think the maps drawn by BaseMap are not very beautiful. Another important reason is that it is difficult to install and may have compatibility problems.
  • Pyecharts is based on Baidu open source JS library Echarts, its biggest characteristics are: simple, easy to install, easy to use.

So we decided to use PyEcharts to draw the map, and the core code is as follows:

The official documentation of Pyecharts (https://pyecharts.org/#/zh-cn/) lists the methods and parameters for drawing various charts in detail. Most importantly, the documentation provides a variety of reference demos to facilitate our faster implementation of the function.

At this point, all of the code is implemented, and I count, without comments, 40 lines of code. Isn’t that simple? Isn’t that powerful? The last rendering we implemented:

It’s left for you to implement

The best way to master a topic is to practice it. Here are two small requirements:

  • To realize a broken line chart showing the trend of daily newly confirmed cases in China except Hubei province.
  • Create a bar chart of daily changes in the number of new cases in your city.

If you’re finished, post the picture and share it with us. Planet intellectual images posted on the corresponding discussion topic (https://t.zsxq.com/Fuj2fY3), which number WeChat public reply message seems to be no way to reply.

Python Automation and Programming Practice MaterialsCopy the code