一、实验目的
- 了解并掌握网络爬虫的基本原理
- 动手实现动态获取全国新型冠状病毒疫情现状
- Windows10
- Pycharm 2020.1
- python 3.7
- 数据来源:https://news.qq.com/zt2020/page/feiyan.htm#/
- Requests模块
- Seaborn 绘图
【爬虫|一个简单的网络爬虫-获取全国新型冠状肺炎疫情】
文章图片
对应的响应信息如下所示:
文章图片
使用json格式化,将获取的响应信息展开:
文章图片
截取出需要的数据字段标识:
文章图片
五、请求
- 通过Requests模块对网页发送请求,确定url地址,通过数据表看到时间戳为8位,*1000补足11位,进行实时获取。
- Url网址:https://news.qq.com/zt2020/page/feiyan.htm#/
- 确定需要提取的参数为:children(目标集合)、name(省份名称),total.confirm(累计确诊病例)、total.dead(累计死亡)、total.suspect(累计疑似人数)、total.heal(累计治愈人数)、today.confirm(新增确诊人数)以上6个参数为本次我们需要获取的目标参数。
- 通过json.loads获取实时的json数据。
文章图片
代码和程序设计
import time, json, requests
# 抓取腾讯疫情实时json数据
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
data = https://www.it610.com/article/json.loads(requests.get(url=url).json()['data'])
#print(data)
#print(data.keys())num = data['areaTree'][0]['children']# 解析数据(确诊 疑似 死亡 治愈)
total_data = https://www.it610.com/article/{}
for item in num:
if item['name'] not in total_data:
total_data.update({item['name']:0})
# print(item['name'])
for city_data in item['children']:
# print(city_data )
total_data[item['name']] += city_data['total']['confirm']
# 省份名称
names = list(total_data.keys())
# 确诊数据
num1 = list(total_data.values())for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]="确诊人数:"+str(total_data[item['name']])
# print(total_data)#解析疑似数据
total_suspect_data = https://www.it610.com/article/{}
for item in num:
if item['name'] not in total_suspect_data:
total_suspect_data.update({item['name']: 0})
for city_data in item['children']:
total_suspect_data[item['name']] += int(city_data['total']['suspect'])
# 疑似数据
num2 = list(total_suspect_data.values())
for item in num:
total_data[item['name']]=total_data[item['name']]+",疑似病例:"+str(total_suspect_data[item['name']])
# print(total_data)
# 解析死亡数据
total_dead_data = https://www.it610.com/article/{}
for item in num:
if item['name'] not in total_dead_data:
total_dead_data.update({item['name']: 0})
for city_data in item['children']:
total_dead_data[item['name']] += int(city_data['total']['dead'])
# 死亡数据
num3 = list(total_dead_data.values())for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]=total_data[item['name']]+",死亡数据:"+str(total_dead_data[item['name']])# 解析治愈数据
total_heal_data = https://www.it610.com/article/{}
for item in num:
if item['name'] not in total_heal_data:
total_heal_data.update({item['name']: 0})
for city_data in item['children']:
total_heal_data[item['name']] += int(city_data['total']['heal'])
# 治愈数据
num4 = list(total_heal_data.values())for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]=total_data[item['name']]+",治愈数据:"+str(total_heal_data[item['name']])# 解析新增确诊数据
total_new_data = https://www.it610.com/article/{}
for item in num:
if item['name'] not in total_new_data:
total_new_data.update({item['name']: 0})
for city_data in item['children']:
total_new_data[item['name']] += int(city_data['today']['confirm'])# today# 新增确诊病例
num5 = list(total_new_data.values())
for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]=total_data[item['name']]+",新增确诊人数:"+str(total_new_data[item['name']])
# print(total_data)
for i,j in total_data.items():
print(i," ",j)
运行结果
文章图片
绘图 由于具有三类型的数据类型,为了将数据写入再同一个表中,使用Seaborn进行图像绘制
准备工作
数据写入,接上代码,这里因为武汉的数据较大,会影响到其他的图表,我直接跳过了湖北,湖北为i=13号。
print(names)
print(num1)
print(num2)
print(num3)
print(num4)
print(num5)# 获取当前日期命名
n = time.strftime("%Y-%m-%d") + "-data_2.csv"
fw = open(n, 'w', encoding='utf-8')
fw.write('省份,类型,人数\n')
i = 0
while i < len(names):
if (i!=13):
fw.write(names[i] + ',累计确诊,' + str(num1[i]) + '\n')
fw.write(names[i] + ',累计死亡,' + str(num3[i]) + '\n')
fw.write(names[i] + ',累计治愈,' + str(num4[i]) + '\n')
fw.write(names[i] + ',新增确诊,' + str(num5[i]) + '\n')
i = i + 1
else:
print("跳过武汉!")
i = i + 1
else:
print("Over !")
fw.close()
图表绘制:
# 调用Seaborn绘制柱状图
import time
import matplotlib
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt# 读取数据
n=time.strftime("%Y-%m-%d")+"-data_2.csv"
data=https://www.it610.com/article/pd.read_csv(n)# 设置窗口
fig,ax=plt.subplots(1,1)
print(data['省份'])# 设置绘图风格及字体
sns.set_style("whitegrid",{'font.sans-serif':['simhei','Arial']})# 绘制柱状图
g=sns.barplot(x='省份',y='人数',hue="类型",data=https://www.it610.com/article/data,ax=ax,palette=sns.color_palette("hls",8))# 设置Axes的标题
ax.set_title('湖北外-中国33省份累计病例对比图')# 设置坐标轴文字方向
ax.set_xticklabels(ax.get_xticklabels(),rotation=-60)# 设置坐标轴刻度的字体大小
ax.tick_params(axis='x',labelsize=8)
ax.tick_params(axis='y',labelsize=8)
plt.show()
结果
文章图片
推荐阅读
- Python|用python爬取全国和全球疫情数据,并进行可视化分析(过程详细代码可运行)
- Python爬虫|每日一练(Python爬虫爬取全国新冠肺炎疫情数据实例详解,使用beautifulsoup4库实现)
- Python爬虫|python爬虫(网易新冠疫情数据爬取(一))
- Python日常小操作|Python爬取疫情数据
- 介绍Python字符串下标(索引)
- 认识Python中的字符串
- Python字符串切片操作和切片含义
- 【Python字符串方法 】大小写转换、删除空白字符、字符串对齐
- Python字符串6个判断操作方法