From fb254c4144eb05277897c8f56cf1952cb44803fc Mon Sep 17 00:00:00 2001 From: hnu202209100109 <2453373680@qq.com> Date: Thu, 21 Dec 2023 15:48:42 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E7=96=AB=E6=83=85=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=88=AC=E8=99=AB.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 疫情数据爬虫.py | 116 +++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/疫情数据爬虫.py b/疫情数据爬虫.py index 4bd9e18..8eaf5fb 100644 --- a/疫情数据爬虫.py +++ b/疫情数据爬虫.py @@ -1,60 +1,58 @@ -# -*- coding: utf-8 -*- -""" -Created on Tue Dec 12 15:24:14 2023 - -@author: Toon -""" - -import requests -from lxml import etree -import csv -import pandas as pd -import matplotlib.pyplot as plt - - -# 请求的url -url = "https://www.bitpush.news/covid19/" -# 设置请求头信息 -headers = { - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" -} -# 使用reqeusts模快发起 GET 请求 -response = requests.get(url, headers=headers) -# 获取请求的返回结果 -html = response.text -parse = etree.HTMLParser(encoding='utf-8') # 添加编码 -# 解析 requests 返回的响应结果 -doc = etree.HTML(html) -state = doc.xpath('//html/body/div/div/div[2]/div[2]/div/div/div/div//tbody/tr/td/span/text()') -# 确诊人数 -person = doc.xpath('//html/body/div/div/div[2]/div[2]/div/div/div/div//tbody/tr/td[2]/text()') -# 由于确诊人数中有逗号,我们使用列表推导式删除 -person = [x.replace(",", "") for x in person] -# 死亡人数 -death = doc.xpath('//html/body/div/div/div[2]/div[2]/div/div/div/div//tbody/tr/td[3]/text()') -# 同样使用列表推导式删除逗号 -death = [x.replace(",", "") for x in death] -message = list(zip(state, person, death)) -with open("content.csv", "w") as f: - w = csv.writer(f) - w.writerows(message) -df = pd.read_csv("content.csv", names=["country", "person", "death"],encoding='gbk') -df.head() -df.info() -df1 = df.head(15) -df1 = df1[::-1] -# 在jupyter中直接展示图像 -#%matplotlib inline -# 设置中文显示 -plt.rcParams['font.sans-serif'] = ['SimHei'] -plt.rcParams['figure.figsize'] = (5,5) # 设置figure_size尺寸 - -#制作柱形图 -x = df1["country"].values -y = df1["person"].values -plt.barh(x, y) -plt.ylabel("国家",fontsize=14) -plt.xlabel("确诊人数",fontsize=14) -for x,y in zip(y,x): - plt.text(x, y,x, ha='left', va='center',color='r') +# -*- coding: utf-8 -*- +""" +Created on Tue Dec 12 15:24:14 2023 +@author: Toon +""" + +import requests +from lxml import etree +import csv +import pandas as pd +import matplotlib.pyplot as plt + + +# 请求的url +url = "https://www.bitpush.news/covid19/" +# 设置请求头信息 +headers = { + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" +} +# 使用reqeusts模快发起 GET 请求 +response = requests.get(url, headers=headers) +# 获取请求的返回结果 +html = response.text +parse = etree.HTMLParser(encoding='utf-8') # 添加编码 +# 解析 requests 返回的响应结果 +doc = etree.HTML(html) +# 地区名称 +state = doc.xpath('//html/body/div/div/div[2]/div[2]/div/div/div/div//tbody/tr/td/span/text()') +# 确诊人数 +person = doc.xpath('//html/body/div/div/div[2]/div[2]/div/div/div/div//tbody/tr/td[2]/text()') +# 由于确诊人数中有逗号,我们使用列表推导式删除 +person = [x.replace(",", "") for x in person] +# 死亡人数 +death = doc.xpath('//html/body/div/div/div[2]/div[2]/div/div/div/div//tbody/tr/td[3]/text()') +# 同样使用列表推导式删除逗号 +death = [x.replace(",", "") for x in death] +message = list(zip(state, person, death)) +with open("content.csv", "w") as f: + w = csv.writer(f) + w.writerows(message) +df = pd.read_csv("content.csv", names=["country", "person", "death"],encoding='gbk') +df.head() +df.info() +df1 = df.head(15) +df1 = df1[::-1] +# 设置中文显示 +plt.rcParams['font.sans-serif'] = ['SimHei'] +plt.rcParams['figure.figsize'] = (5,5) # 设置figure_size尺寸 + +#制作柱形图 +x = df1["country"].values +y = df1["person"].values +plt.barh(x, y) +plt.ylabel("国家",fontsize=14) +plt.xlabel("确诊人数",fontsize=14) +for x,y in zip(y,x): + plt.text(x, y,x, ha='left', va='center',color='r') plt.show() \ No newline at end of file