[Python crawler] crawling US epidemic data + tabulation from Tencent API

Recently (the article was written at 18:40 on 2020/6/1), the epidemic situation has improved in China, but it has been violent in the United States.
This article will crawl and tabulate the US epidemic data provided by Tencent .

1. Crawl data

Call API interface

Interface: https://api.inews.qq.com/newsqa/v1/automation/modules/list?modules=FAutoCountryMerge
Observed data:

{
    
    
	...,
	"data": {
    
    
		"FAutoCountryMerge": {
    
    
			...,
			"美国": {
    
    
				"showDash":false,
				"list": [
					{
    
    "date":"01.28","confirm_add":0,"confirm":5,"heal":0,"dead":0},
					...,
					{
    
    "date":"05.29","confirm_add":25069,"confirm":1768461,"heal":510713,"dead":103330},
					{
    
    "date":"05.30","confirm_add":23290,"confirm":1793530,"heal":519569,"dead":104542},
					{
    
    "date":"05.31","confirm_add":20350,"confirm":1816820,"heal":535238,"dead":105557},
					{
    
    "date":"06.01","confirm_add":20350,"confirm":1837170,"heal":599867,"dead":106195}
				]
			},
			...
		}
	}
}

As shown in the code above, for a country, to obtain its epidemic data only need to use:

json['data']['FAutoCountryMerge']['<国名>']['list']

For US data, use:

json['data']['FAutoCountryMerge']['美国']['list']

Code

The above are all dry goods, but the following is the real one code:

from requests import get

url = 'https://api.inews.qq.com/newsqa/v1/automation/modules/list?modules=FAutoCountryMerge'
data = get(url).json()['data']['FAutoCountryMerge']['美国']['list']

Data processing

In python, the result is an listobject:

[
	{
    
    "date":"01.28","confirm_add":0,"confirm":5,"heal":0,"dead":0},
	...,
	{
    
    "date":"05.29","confirm_add":25069,"confirm":1768461,"heal":510713,"dead":103330},
	{
    
    "date":"05.30","confirm_add":23290,"confirm":1793530,"heal":519569,"dead":104542},
	{
    
    "date":"05.31","confirm_add":20350,"confirm":1816820,"heal":535238,"dead":105557},
	{
    
    "date":"06.01","confirm_add":20350,"confirm":1837170,"heal":599867,"dead":106195}
]

The object stores the daily epidemic data in the United States
date,: the date from January 28 to the present;: the
confirm_addnewly confirmed diagnosis on
confirmthis day;: the cumulative diagnosis on
healthis day;: the cumulative cure on
deadthis day;: the cumulative death on this day.

Filter data

Data filtering is very important.

  • confirm_add(Newly diagnosed that day) Obviously useless, remove
  • One should be added now_confirm(existing diagnoses on that day), so that we can clearly see the number of people under treatment in the United States.
    This value can be confirm - heal - headobtained through .

date: the date from January 28 to the present
confirm_add: newly confirmed diagnoses on this day
confirm: cumulative diagnoses on
that day heal: cumulative cures on
that day dead: cumulative deaths on
that day now_confirm: existing confirmed diagnoses on that day

Code

Because the number of people in the front is too small, the data will affect the quality of the final drawing.
So, I save the data from the 35th, of course, if you want to use all the data, you can data[35:]change it data.

dates = []
confirms = []
now_confirms = []
heals = []
deads = []

for day_data in data[35:]:
    dates.append(day_data['date'])
    confirms.append(day_data['confirm'])
    heals.append(day_data['heal'])
    deads.append(day_data['dead'])
    now_confirms.append(confirms[-1] - heals[-1] - deads[-1])

2. Drawing

Reference article: https://www.cnblogs.com/lone5wolf/p/10870200.html
Since I am still a novice in drawing, I post the code directly, please understand. . .

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# 绘制文本
plt.figure(figsize=(11.4, 7.7))

confirm_line, = plt.plot(dates, confirms, color='#8B0000')
now_confirm_line, = plt.plot(dates, now_confirms, color='red', linestyle=':')
heal_line, = plt.plot(dates, heals, color='green', linestyle='--')
dead_line, = plt.plot(dates, deads, color='black', linestyle='-.')

# 绘制图形
my_font = FontProperties(fname=r'fonts\msyh.ttc')
plt.legend(handles=[confirm_line, now_confirm_line, heal_line, dead_line], labels=['累计确诊', '现存确诊', '治愈', '死亡'], prop=my_font)
plt.xlabel('日期', fontproperties=my_font)
plt.ylabel('人数', fontproperties=my_font)
plt.title('美国2019-nCov疫情情况', fontproperties=my_font)
plt.gca().xaxis.set_major_locator(plt.MultipleLocator(7))

# 保存并显示统计图
plt.savefig('AmericaNCovData.png')
plt.show()

Result picture

C nCov

3. Complete code

# -*- coding: utf-8 -*-
from requests import get
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

url = 'https://api.inews.qq.com/newsqa/v1/automation/modules/list?modules=FAutoCountryMerge'
data = get(url).json()['data']['FAutoCountryMerge']['美国']['list']

dates = []
confirms = []
now_confirms = []
heals = []
deads = []

for day_data in data[35:]:
    dates.append(day_data['date'])
    confirms.append(day_data['confirm'])
    heals.append(day_data['heal'])
    deads.append(day_data['dead'])
    now_confirms.append(confirms[-1] - heals[-1] - deads[-1])

# 绘制文本
plt.figure(figsize=(11.4, 7.7))

confirm_line, = plt.plot(dates, confirms, color='#8B0000')
now_confirm_line, = plt.plot(dates, now_confirms, color='red', linestyle=':')
heal_line, = plt.plot(dates, heals, color='green', linestyle='--')
dead_line, = plt.plot(dates, deads, color='black', linestyle='-.')

# 绘制图形
my_font = FontProperties(fname=r'fonts\msyh.ttc')
plt.legend(handles=[confirm_line, now_confirm_line, heal_line, dead_line], labels=['累计确诊', '现存确诊', '治愈', '死亡'], prop=my_font)
plt.xlabel('日期', fontproperties=my_font)
plt.ylabel('人数', fontproperties=my_font)
plt.title('美国2019-nCov疫情情况', fontproperties=my_font)
plt.gca().xaxis.set_major_locator(plt.MultipleLocator(7))

# 保存并显示统计图
plt.savefig('AmericaNCovData.png')
plt.show()

Code download: GitHub

Guess you like

Origin blog.csdn.net/write_1m_lines/article/details/106479530