前面两期,我们分别介绍了疫情地图DIY—数据获取和疫情地图DIY—全国地图。可能有人更关心湖北省的疫情情况,毕竟这次疫情首先发生在武汉,并且周围的黄冈市和孝感市都是重灾区。那么今天我们就来画各省市的疫情地图。
第一步还是获取数据,在疫情地图DIY—数据获取一文中,我们讲过了如何获取省份的疫情数据。实际上,抓取的网页里面已经包含了城市的数据,只是我们没有用正则表达式提取而已。那么今天我们就先来获取每个城市的疫情数据。我还是用了R和Perl两种方法来实现。
Perl获取城市疫情数据:
#!/usr/bin/perl -w
use LWP::Simple qw(get);
use utf8;
use Encode;
#获取丁香园网页信息
$content=get("https://3g.dxy.cn/newh5/view/pneumonia");
#疫情信息写入到2019-ncov_cities.csv中
open OUT,">2019-ncov_cities.csv";
#表头信息
print OUT "province,city,confirmed,suspected,cured,dead\n";
#利用正则表达式提取省份名称
while($content=~/"provinceName":"([^"]+)","provinceShortName":"([^"]+)","confirmedCount":(\d+),"suspectedCount":(\d+),"curedCount":(\d+),"deadCount":(\d+),".*?"cities":\[(.*?)\]}/sg){
$cities=$7;
$province=$1;
#利用正则表达式提取城市名称,确诊,死亡和治愈的人数
while($cities=~/"cityName":"([^"]+)","confirmedCount":(\d+),"suspectedCount":(\d+),"curedCount":(\d+),"deadCount":(\d+)/sg){
$a="市";
#城市信息连同省份名字一起输出
#在城市名称后面加一个'市',网页上获取的城市名称为'武汉’,没有“市”
$line="$province,$1$a,$2,$3,$4,$5";
$line=encode("gb2312",$line);
print OUT "$line\n";
}
}
打开2019-ncov_cities.csv查看城市信息
R获取城市疫情数据:
#从丁香园抓取网页信息
web_page <- readLines("https://3g.dxy.cn/newh5/view/pneumonia")
#获取包含疫情数据的行
info_row<- web_page[grep("getAreaStat", web_page)]
info_row=sub('.*<script id="getAreaStat">(.*?)</script>.*',"\\1",perl=T,info_row)
#定义提取城市疫情数据的函数extract
extract=function(x,province){
a=sub('.*"cityName":"([^"]+)","confirmedCount":(\\d+),"suspectedCount":(\\d+),"curedCount":(\\d+),"deadCount":(\\d+).*', "\\1:\\2:\\3:\\4:\\5", x ,perl=TRUE)
c(province,unlist(strsplit(a,":")))
}
#获取城市疫情数据
city=c()
#将疫情数据分成34个省份
provinces=strsplit(info_row,"]},",fixed=T)
#从每个省份提取该省份中城市疫情的数据
for(i in provinces[[1]]){
province=sub('.*"provinceName":"([^"]+)","provinceShortName":"([^"]+)".*', "\\2", i ,perl=TRUE)
if(length(grep('.*"cityName":"([^"]+)","confirmedCount":(\\d+),"suspectedCount":(\\d+),"curedCount":(\\d+),"deadCount":(\\d+).*',i))>0){
cities=strsplit(i,'},{',fixed=T)
b=do.call(rbind,lapply(cities[[1]],extract,province=province))
city=rbind(city,b)
}
}
city=as.data.frame(city)
#定义表头
names(city)=c("province","city","confirmed","suspected","cured","dead")
city$province=iconv(city$province,"UTF-8",'GB2312')
#在城市名称后面加一个'市',网页上获取的城市名称为'武汉’,没有“市”
b="市"
b=iconv(b,"UTF-8",'GB2312')
city$city=paste(iconv(as.character(city$city),"UTF-8",'GB2312'),b,sep="")
#将数据写入到csv文件中
write.csv(file="city.csv",city,quote=F,row.names=F)
打开city.csv查看城市疫情数据
下面还是使用疫情地图DIY—全国地图里面用到的R包REmap来画各省市疫情地图,这里以湖北省为例。
联系客服