每日一练:Python爬虫爬取全国新冠肺炎疫情数据实例详解,使用beautifulsoup4库实现
生活随笔
收集整理的這篇文章主要介紹了
每日一练:Python爬虫爬取全国新冠肺炎疫情数据实例详解,使用beautifulsoup4库实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Python 爬蟲篇 - 爬取全國新冠肺炎疫情數(shù)據(jù)實例詳解
- 效果圖展示
- 第一章:疫情信息的下載與數(shù)據(jù)提取
- ① 爬取頁面數(shù)據(jù)到本地
- ② json 字符串正則表達式分析
- ③ 提取數(shù)據(jù)中的 json 字符串
- 第二章:疫情信息數(shù)據(jù)分析
- ① 提取 json 字符串里的省份疫情數(shù)據(jù)并顯示
- ② 顯示查詢省份的城市疫情數(shù)據(jù)
[ 系列文章篇 ]
Python 地圖篇 - 使用 pyecharts 繪制世界地圖、中國地圖、省級地圖、市級地圖實例詳解
[ 專欄推薦 ]
Python 短視頻自動化發(fā)布,包含抖音、快手、bilibili、小紅書、微視、好看視頻、西瓜視頻、微信視頻號等 10 余種平臺
效果圖展示
這是省份的:
這是城市的:
第一章:疫情信息的下載與數(shù)據(jù)提取
① 爬取頁面數(shù)據(jù)到本地
通過 BeautifulSoup 庫解析代碼,將疫情信息內(nèi)容下載到本地 txt 文件用于數(shù)據(jù)分析使用。
下載后的數(shù)據(jù)
② json 字符串正則表達式分析
通過分析文件查找到
需要數(shù)據(jù)的 json 字符串前關(guān)鍵詞 "try { window.getAreaStat = "
json 字符串后關(guān)鍵詞 }catch(e){}
(.*?) 是匹配所有內(nèi)容。
整合的正則表達式為如下:
③ 提取數(shù)據(jù)中的 json 字符串
讀取本地文件,提取里面的 json 字符串數(shù)據(jù)。
import redef get_json():"""xiaolanzao, 2022.02.27【作用】讀取本地文件,獲取json信息【參數(shù)】無【返回】json字符串"""# 讀取本地文件f = open("疫情數(shù)據(jù).txt", "r", encoding="utf-8")f_content = f.read()f.close()# json字符串前后關(guān)鍵詞json_start = "try { window.getAreaStat = "# 字符串包含的括號要進行轉(zhuǎn)義json_end = "}catch\(e\){}"# json字符串正則匹配# (.*?)是匹配所有內(nèi)容regular_key = json_start + "(.*?)" + json_end# 參數(shù)rs.S可以無視換行符,將所有文本視作一個整體進行匹配re_content = re.search(regular_key, f_content, re.S)# group()用于獲取正則匹配后的字符串content = re_content.group()# 去除json字符串的前后關(guān)鍵詞content = content.replace(json_start, '')# 尾巴要去掉轉(zhuǎn)義符號json_end = "}catch(e){}"content = content.replace(json_end, '')print(content)return contentjson_content = get_json()讀取后的內(nèi)容:
第二章:疫情信息數(shù)據(jù)分析
① 提取 json 字符串里的省份疫情數(shù)據(jù)并顯示
方法里所傳入的數(shù)據(jù)是上面返回的 json 字符串。
import jsondef display_provinces(json_content):"""xiaolanzao, 2022.02.27【作用】展示省份疫情【參數(shù)】json_content : json字符串【返回】無"""# 將字符串轉(zhuǎn)化為字典json_data = json.loads(json_content)# 省份數(shù)據(jù)展示print("全國各省份疫情數(shù)據(jù)如下:")for i in json_data:print("【省份名】:" + i["provinceName"])print("現(xiàn)存確診:" + str(i["currentConfirmedCount"]))print("累計確診:" + str(i["confirmedCount"]))print("死亡:" + str(i["deadCount"]))print("治愈:" + str(i["curedCount"]))print()display_provinces(json_content)運行效果圖:
可以對比下數(shù)據(jù)是一致的
② 顯示查詢省份的城市疫情數(shù)據(jù)
城市數(shù)據(jù)在省份數(shù)據(jù)的 cities 里面。
import jsondef display_citys(json_content, province_name):"""xiaolanzao, 2022.02.27【作用】展示城市疫情【參數(shù)】json_content : json字符串province_name : 需要查詢的省份名【返回】無"""# 將字符串轉(zhuǎn)化為字典json_data = json.loads(json_content)# 省份數(shù)據(jù)展示print(province_name + "疫情數(shù)據(jù)如下:")for i in json_data:# print(i)if(i["provinceName"] == province_name):# 讀取里面的城市信息try:citys = i["cities"]for ii in citys:print("【城市名】:" + ii["cityName"])print("現(xiàn)存確診:" + str(ii["currentConfirmedCount"]))print("累計確診:" + str(ii["confirmedCount"]))print("死亡:" + str(ii["deadCount"]))print("治愈:" + str(ii["curedCount"]))print()except Exception as e:print(e)print("沒有相應(yīng)的城市信息!")display_citys(json_content, "河北省")運行效果圖:
可以對比下數(shù)據(jù)是一致的
喜歡的點個贊?吧!
總結(jié)
以上是生活随笔為你收集整理的每日一练:Python爬虫爬取全国新冠肺炎疫情数据实例详解,使用beautifulsoup4库实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 代码绘制航海王四皇大妈(B
- 下一篇: SQL Server2019数据库查询所