爬虫项目(三)---采集最近一日全国各省疫情数据

该内容出自黑马程序员教程

采集最近一日全国各省疫情数据

当然,数据来源仍然是丁香园新型冠状病毒肺炎疫情实时动态首页
url:https://ncov.dxy.cn/ncovh5/view/pneumonia

思路:首先需要先确定全国各省疫情数据的位置

在这里插入图片描述
全国各省份的疫情数据信息都在id="getAreaStat"

步骤:

  1. 发送请求,获取疫情首页内容
  2. 解析疫情首页内容,获取最近一日各省疫情信息
  3. 以json格式保存疫情信息
import requests
import re
import json
from bs4 import BeautifulSoup
from tqdm import tqdm#进度条class CoronaSpider(object):def __init__(self):self.home_url = 'https://ncov.dxy.cn/ncovh5/view/pneumonia'def get_content_from_url(self,url):#根据URL获取响应内容的字符串数据#URL:请求的URL#返回:响应内容的字符串response = requests.get(url)return response.content.decode()def parse_home_page(self,home_page): #解析首页内容,获取解析后的Python数据#home_page:首页内容#返回:解析后的Python类型数据#2,从疫情首页提取最近一日各国疫情数据soup = BeautifulSoup(home_page,'lxml')script = soup.find(id='getListByCountryTypeService2true')text = script.text#print(text)#3,从疫情数据中获取json格式的字符串json_str = re.findall(r'\[.+\]',text)[0]#由于中括号是个特殊的字符,需要在前面加个转义符;最后的结果会存在列表中,故使用[0]来获取完整json格式#print(json_str)#4,把json格式的字符串转换为Python类型data = json.loads(json_str)#print(last_day_nature_num)return datadef save(self,data,path):#5,以json格式保存最近一日各国疫情数据with open(path,'w') as fp:json.dump(data,fp)#,ensure_ascii=Falsec'''def save(self,data):#5,以json格式保存最近一日各国疫情数据with open('yy1.json','w') as fp:json.dump(data,fp)#,ensure_ascii=False'''def crawl_last_day_corona_virus(self):#采集最近一天各国疫情信息#1,发送请求,获取首页内容home_page = self.get_content_from_url(self.home_url)#2,解析首页内容,获取最近一天的各国疫情数据last_data_corona_virus = self.parse_home_page(home_page)#3,保存数据self.save(last_data_corona_virus,'E:\Jupyter_workspace\study\python\爬虫\last_day_nature_num111.json')def crawl_corona_virus(self):#采集从01月23号以来的世界各国疫情数据#1,加载最近一日各国疫情数据#with open('yy1.json') as fp:with open('E:\Jupyter_workspace\study\python\爬虫\last_day_nature_num111.json') as fp:last_day_corona_virus = json.load(fp)#print(last_day_corona_virus)#定义列表,用于存储各国从1月23号以来的疫情数据corona_virus = []#2,遍历各国疫情数据,获取从01月23号以来的世界各国疫情的URLfor country in tqdm(last_day_corona_virus,'获取从01月23号以来的世界各国疫情信息'):statustics_data_url = country['statisticsData']#3,发送请求,获取从01月23号以来的世界各国疫情的json字符串statustics_data_json_str = self.get_content_from_url(statustics_data_url)#4,解析各个国家疫情的json字符串,转化为Python类型数据,添加到列表中statustics_data = json.loads(statustics_data_json_str)['data']#print(statustics_data)for one_day in statustics_data:#statustics_data这个数据里面没有国家的一些信息,需要补充上去one_day['provinceName'] = country['provinceName']one_day['countryShortCode'] = country['countryShortCode']#print(statustics_data)corona_virus.extend(statustics_data)#把每个国家的疫情信息statustics_data,都添加到一个大的corona_virus列表里面#5,将该列表以json格式保存从01月23号以来的世界各国疫情数据信息self.save(corona_virus,'E:\Jupyter_workspace\study\python\爬虫\corona_virus.json')def craw_last_day_corona_virus_of_china(self):#采集最近一日国内各省疫情数据#1,发送请求,获取疫情首页信息home_page = self.get_content_from_url(self.home_url)#2,解析疫情首页信息,获取最近一日各省疫情数据soup = BeautifulSoup(home_page,'lxml')script = soup.find(id='getAreaStat')text = script.text#print(text)#从疫情数据中获取json格式的字符串json_str = re.findall(r'\[.+\]',text)[0]#由于中括号是个特殊的字符,需要在前面加个转义符;最后的结果会存在列表中,故使用[0]来获取完整json格式#print(json_str)#把json格式的字符串转换为Python类型data = json.loads(json_str)#print(last_day_nature_num)#3,保存疫情数据self.save(data,'E:\Jupyter_workspace\study\python\爬虫\craw_last_day_corona_virus_of_china.json')def run(self):#self.crawl_last_day_corona_virus()#self.crawl_corona_virus()self.craw_last_day_corona_virus_of_china()if  __name__ == '__main__':spider = CoronaSpider()spider.run()

很显然,这里的craw_last_day_corona_virus_of_china和parse_home_page有的步骤是相同的,接下来开始代码的重构进行优化

import requests
import re
import json
from bs4 import BeautifulSoup
from tqdm import tqdm#进度条class CoronaSpider(object):def __init__(self):self.home_url = 'https://ncov.dxy.cn/ncovh5/view/pneumonia'def get_content_from_url(self,url):#根据URL获取响应内容的字符串数据#URL:请求的URL#返回:响应内容的字符串response = requests.get(url)return response.content.decode()def parse_home_page(self,home_page,tag_id): #解析首页内容,获取解析后的Python数据#home_page:首页内容#返回:解析后的Python类型数据#2,从疫情首页提取最近一日各国疫情数据soup = BeautifulSoup(home_page,'lxml')script = soup.find(id=tag_id)text = script.text#print(text)#3,从疫情数据中获取json格式的字符串json_str = re.findall(r'\[.+\]',text)[0]#由于中括号是个特殊的字符,需要在前面加个转义符;最后的结果会存在列表中,故使用[0]来获取完整json格式#print(json_str)#4,把json格式的字符串转换为Python类型data = json.loads(json_str)#print(last_day_nature_num)return datadef save(self,data,path):#5,以json格式保存最近一日各国疫情数据with open(path,'w') as fp:json.dump(data,fp)#,ensure_ascii=Falsec'''def save(self,data):#5,以json格式保存最近一日各国疫情数据with open('yy1.json','w') as fp:json.dump(data,fp)#,ensure_ascii=False'''def crawl_last_day_corona_virus(self):#采集最近一天各国疫情信息#1,发送请求,获取首页内容home_page = self.get_content_from_url(self.home_url)#2,解析首页内容,获取最近一天的各国疫情数据last_data_corona_virus = self.parse_home_page(home_page,tag_id='getListByCountryTypeService2true')#3,保存数据self.save(last_data_corona_virus,'E:\Jupyter_workspace\study\python\爬虫\last_day_nature_num111.json')def crawl_corona_virus(self):#采集从01月23号以来的世界各国疫情数据#1,加载最近一日各国疫情数据#with open('yy1.json') as fp:with open('E:\Jupyter_workspace\study\python\爬虫\last_day_nature_num111.json') as fp:last_day_corona_virus = json.load(fp)#print(last_day_corona_virus)#定义列表,用于存储各国从1月23号以来的疫情数据corona_virus = []#2,遍历各国疫情数据,获取从01月23号以来的世界各国疫情的URLfor country in tqdm(last_day_corona_virus,'获取从01月23号以来的世界各国疫情信息'):statustics_data_url = country['statisticsData']#3,发送请求,获取从01月23号以来的世界各国疫情的json字符串statustics_data_json_str = self.get_content_from_url(statustics_data_url)#4,解析各个国家疫情的json字符串,转化为Python类型数据,添加到列表中statustics_data = json.loads(statustics_data_json_str)['data']#print(statustics_data)for one_day in statustics_data:#statustics_data这个数据里面没有国家的一些信息,需要补充上去one_day['provinceName'] = country['provinceName']one_day['countryShortCode'] = country['countryShortCode']#print(statustics_data)corona_virus.extend(statustics_data)#把每个国家的疫情信息statustics_data,都添加到一个大的corona_virus列表里面#5,将该列表以json格式保存从01月23号以来的世界各国疫情数据信息self.save(corona_virus,'E:\Jupyter_workspace\study\python\爬虫\corona_virus.json')def craw_last_day_corona_virus_of_china(self):#采集最近一日国内各省疫情数据#1,发送请求,获取疫情首页信息home_page = self.get_content_from_url(self.home_url)craw_last_day_corona_virus_of_china = self.parse_home_page(home_page,tag_id='getAreaStat')'''#2,解析疫情首页信息,获取最近一日各省疫情数据soup = BeautifulSoup(home_page,'lxml')script = soup.find(id='getAreaStat')text = script.text#print(text)#从疫情数据中获取json格式的字符串json_str = re.findall(r'\[.+\]',text)[0]#由于中括号是个特殊的字符,需要在前面加个转义符;最后的结果会存在列表中,故使用[0]来获取完整json格式#print(json_str)#把json格式的字符串转换为Python类型data = json.loads(json_str)#print(last_day_nature_num)'''#3,保存疫情数据self.save(craw_last_day_corona_virus_of_china,'E:\Jupyter_workspace\study\python\爬虫\craw_last_day_corona_virus_of_china.json')def run(self):#self.crawl_last_day_corona_virus()#self.crawl_corona_virus()self.craw_last_day_corona_virus_of_china()if  __name__ == '__main__':spider = CoronaSpider()spider.run()

raw_last_day_corona_virus_of_china.json文件内容如下:
在这里插入图片描述
这里的编码格式没有改变,故各个国家的汉字名称没有出现
在这里插入图片描述
https://file1.dxycdn.com/2020/0223/331/3398299755968040033-135.json该json文件中存放着全国各个省的疫情数据信息。
爬虫项目(四)中会用到该信息。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378189.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机专业博士后排名,排名丨计算机专业领域TOP10,性价比超高!

原标题:排名丨计算机专业领域TOP10,性价比超高!相信各位家长、同学已经看过太多专业的排名,我问过很多理科生将来想学什么专业,听到频率最高的还是计算机专业。似乎大家都知道,学计算机是比较挣钱的&#x…

python set |_Python事件类| set()方法与示例

python set |Python Event.set()方法 (Python Event.set() Method) set() is an inbuilt method of the Event class of the threading module in Python. set()是Python中线程模块的Event类的内置方法。 When the set() method is called, the internal flag of that event c…

js 命名规范

转载于:https://www.cnblogs.com/zjx2011/p/3165043.html

爬虫项目(四)---采集从01月22日以来全国各省疫情数据

采集从03月02日以来全国各省疫情数据 当然,数据来源仍然是丁香园新型冠状病毒肺炎疫情实时动态首页 url:https://ncov.dxy.cn/ncovh5/view/pneumonia 分析 确定01月22日以来全国各省疫情数据的URL 由项目(三)可以获取全国各省疫情数据点击可下载&…

Install PHP and Apache

http://cn.php.net/manual/en/install.unix.apache2.php Install Apache first, then sudo apt-get install libxml2-dev sudo apt-get install libmysqlclient16-dev Then, configure PHP转载于:https://www.cnblogs.com/songsiyao/archive/2011/09/15/2178087.html

纠错码trick和数据压缩trick

纠错码和压缩算法是同一枚硬币的两面。 两者都来自于对冗余的想法。 纠错码被视为向消息或文件中添加冗余的原则性方法。而压缩算法正好相反,他们会从消息或文件中移除冗余。 压缩和纠错并不是彼此抵消的,相反,好的压缩算法会移除抵消冗余&am…

计算机专业理论,计算机专业综合理论.doc

计算机专业综合理论2010年南京市单招班教学调研测试卷(二)计算机专业综合理论命题人:管荣平 陈高峰 戴则萍 吴有俊本试卷分第一卷(单项选择题、判断题)和第二卷(填空题、程序阅读题、编程题和计算作图题)两部分。第一卷1至2页,第二卷3至6页。两卷满分300…

网站上flv,MP4等格式的视频文件播放不出来的解决办法

在做一个网站时,发现视频文件,比如flv,MP4格式在本地可以正常的播放,但是传到了开发机器上,就不行了。播放器的文件地址是对的,就是一直没有反应。 经过长时间的实验,发现问题在与iis的设置问题…

centos6 更新数据源

尝试了很多,还是163源最舒服. 编辑yum配置文件(163源): #vi /etc/yum.repos.d/CentOS-Base.repo [base] nameCentOS-$releasever - Base mirrorlisthttp://mirrorlist.centos.org/?release$releasever&arch$basearch&repoos #baseurlhttp://mirror.centos…

常用算法总结(穷举法、贪心算法、递归与分治算法、回溯算法、数值概率算法)

博主联系方式: QQ:1540984562 QQ交流群:892023501 群里会有往届的smarters和电赛选手,群里也会不时分享一些有用的资料,有问题可以在群里多问问。 目录1、穷举法2、贪心算法3、递归与分治算法4、回溯算法5、数值概率算法1、穷举法…

struct/class的数据对齐---简单解析

网上教程一大堆,我这边就不再赘述废话了 思路方法: 1,以四个为一组,最终的内存所占结果必须是四的倍数 2,优先考虑四的整数倍,之后再考虑内存空间问题 struct Beyond{int a;char b;short c;}; int mai…

工程师英语和计算机证书查询,点击进入国家硬件维修工程师证书查询网站

工程师证书查询网站人力资源社会保障部指定查询国家职业资格证书的唯一官方网站。涵盖全国各省市、各行业、各央企颁发的证书。电脑硬件维修工程师网上能查看国家工信部硬件维修工程师证书查询网址:http://www.ceiaec.org/index.htm工程师证书编号在网上怎么查询如果…

stl vector 函数_vector :: at()函数以及C ++ STL中的示例

stl vector 函数C vector :: at()函数 (C vector::at() function) vector::at() is a library function of "vector" header, it is used to access an element from specified position, it accepts a position/index and returns the reference to the element at…

敏捷开发“松结对编程”系列之七:问题集之一

本文是“松结对编程”系列的第七篇。(之一,之二,之三,之四,之五,之六,之七,之八)刚刚参加完MPD 2011深圳站,在演讲中间及后来媒体采访,被问到了一…

powerdesigner 导出数据库表结构

http://www.360doc.com/content/12/0817/19/61497_230730771.shtml转载于:https://www.cnblogs.com/gaohuag/p/3169095.html

C++中的sort函数对二维数组排序是按照什么准则?

遇到的一个疑惑&#xff0c;现记录如下&#xff1a; int main() {vector<vector<int>> envelopes { {5, 8},{6, 7},{6, 4},{2, 3},{8,9} };sort(envelopes.begin(), envelopes.end());for (int i 0;i < envelopes.size();i)cout << envelopes[i][0]<…

Exercises

I. Faulty sentences 1&#xff0c;Our host entertained us with many interesting stories of adventure, he has been a member of an exploration team working in the Arctic. 翻译&#xff1a;我们的主持人用许多有趣的冒险故事来娱乐我们&#xff0c;他是北极探险团队…

数学专业学计算机哪一行,计算数学

计算数学(一个理科专业)语音编辑锁定讨论上传视频计算数学是由数学、物理学、计算机科学、运筹学与控制科学等学科交叉渗透而形成的一个理科专业。中文名计算数学外文名Computational Mathematics所 属数学计算数学专业定义编辑语音计算数学也叫做数值计算方法或数值分析。主…

数论之数字根 杭电1013

做这道题就有一种感觉&#xff0c;&#xff0c;数学真是奇妙&#xff0c;&#xff0c;在网上查了一下&#xff0c;才知道数字根有那么多奇妙的性质。不过&#xff0c;对于这道题我却是不太理解&#xff0c;&#xff0c;主要是不会证明为什么数字根就是各个位加起来对9取余&…

ubuntu12.10下安装mysqlworkbench出现“Dependency is not satisfiable: libctemplate0”问题的解决方案...

(原文地址&#xff1a;http://www.cnblogs.com/Deasel-s-magic-box/p/3169790.html) 之前在window下面一直用navicat&#xff0c;转到ubuntu下之后&#xff0c;虽然也找到一个navicat的linux版&#xff0c;但是经常各种莫名其妙的挂掉&#xff0c;而且界面实在是挫的1B 。 所以…