疫情期间写个一个爬虫代码,爬的是全国各省的疫情状况。然后将情况绘成表格,条形图等。
所包含的库有
from selenium import webdriver #爬虫模拟操作
import requests as res
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt #绘制条形图
from prettytable import PrettyTable #绘制表格所用模块
driver=webdriver.Chrome()
ulr='https://ncov.dxy.cn/ncovh5/view/pneumonia?scene=2&clicktime=1579582470&enterid=1579582470&from=groupmessage&isappinstalled=0' #可爬虫的新冠疫情网站
获取数据
driver.get(ulr)
driver.find_element_by_xpath('//*[@class="expandRow___1Y0WD internalTable___bQT_T"]').click()
mingcheng,now,al,die,cue,tot=[],[],[],[],[],[]
response = res.get(ulr)#provinceName
for i in range(34):#共拥有34个省和自治区
#获得各个省现有,累计,死亡,治愈人数情况
response=driver.find_element_by_xpath(f'//*[@class="areaBox___Sl7gp themeA___1BO7o numFormat___nZ7U7 flexLayout___1pYge"]/div[{i+3}]/div[1]')
html = response.text
html=html.split('\n')
for i in range(len(html)):
html[i]=html[i].translate(str.maketrans('','',','))
if html[0]=='湖北':#因湖北人数过多,图表不易查看,单独列出
hubei=html
continue
mingcheng.append(html[0])
now.append(int(html[1]))
al.append(int(html[2]))
die.append(int(html[3]))
cue.append(int(html[4]))
driver.close()
tot.append(sum(now)+int(hubei[1]))
tot.append(sum(al)+int(hubei[2]))
tot.append(sum(die)+int(hubei[3]))
tot.append(sum(cue)+int(hubei[4]))
然后打印出表格
x = PrettyTable(["名称","现有","累计","死亡","治愈"])
x.add_row([hubei[0],hubei[1],hubei[2],hubei[3],hubei[4]])
for i in range(len(mingcheng)):
x.add_row([mingcheng[i],now[i],al[i],die[i],cue[i]])
print(x)
hubei[1],hubei[2],hubei[3],hubei[4]='现有:'+hubei[1],'累计:'+hubei[2],'死亡:'+hubei[3],'治愈:'+hubei[4]
hubei=hubei[:-1]
output:
+--------+------+-------+------+-------+
| 名称 | 现有 | 累计 | 死亡 | 治愈 |
+--------+------+-------+------+-------+
| 湖北 | 4 | 68143 | 4512 | 63627 |
| 香港 | 134 | 5348 | 105 | 5109 |
| 上海 | 92 | 1200 | 7 | 1101 |
| 新疆 | 64 | 966 | 3 | 899 |
| 四川 | 39 | 757 | 3 | 715 |
| 台湾 | 39 | 567 | 7 | 521 |
| 广东 | 38 | 1938 | 8 | 1892 |
| 福建 | 29 | 443 | 1 | 413 |
| 陕西 | 26 | 456 | 3 | 427 |
| 内蒙古 | 26 | 297 | 1 | 270 |
| 天津 | 15 | 273 | 3 | 255 |
| 甘肃 | 10 | 180 | 2 | 168 |
| 江苏 | 8 | 674 | 0 | 666 |
| 浙江 | 7 | 1287 | 1 | 1279 |
| 河北 | 7 | 373 | 6 | 360 |
| 北京 | 6 | 944 | 9 | 929 |
| 山东 | 5 | 848 | 7 | 836 |
| 山西 | 5 | 215 | 0 | 210 |
| 河南 | 4 | 1284 | 22 | 1258 |
| 重庆 | 3 | 589 | 6 | 580 |
| 辽宁 | 3 | 283 | 2 | 278 |
| 云南 | 3 | 213 | 2 | 208 |
| 湖南 | 1 | 1020 | 4 | 1015 |
| 黑龙江 | 1 | 949 | 13 | 935 |
| 安徽 | 0 | 991 | 6 | 985 |
| 江西 | 0 | 935 | 1 | 934 |
| 广西 | 0 | 260 | 2 | 258 |
| 海南 | 0 | 171 | 6 | 165 |
| 吉林 | 0 | 157 | 2 | 155 |
| 贵州 | 0 | 147 | 2 | 145 |
| 宁夏 | 0 | 75 | 0 | 75 |
| 澳门 | 0 | 46 | 0 | 46 |
| 青海 | 0 | 18 | 0 | 18 |
| 西藏 | 0 | 1 | 0 | 1 |
+--------+------+-------+------+-------+
全国总情况绘制条形图
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']#语言编辑
df1=pd.Series(tot)
df1.plot(kind='bar')
column=['现有', '累计', '死亡', '治愈']
scale_l = range(len(column))
plt.xticks(scale_l,column,rotation=0,size=20)
plt.title('中国总新冠状病情情况',fontsize='20')
plt.show()
全国各省的情况绘图:
l5 = np.array([now,al,die,cue])
l5=l5.T
plt.rcParams['figure.figsize'] = (15,5)
df2 = pd.DataFrame(l5, columns=['现有', '累计', '死亡', '治愈'])
df2.plot(kind='bar') #分开并列线束
scale_ls = range(len(mingcheng))
plt.xticks(scale_ls,mingcheng,rotation=0)
plt.title('中国各省新冠状病情情况',fontsize='20')
plt.show()
for i in hubei:
print(i)
最后输出里还有湖北单独的情况,因为湖北基数较大
output:
湖北
现有:4
累计:68143
死亡:4512
治愈:63627