From e0b9e1a1dac4113ddfe85c6f8e8e7f9ae9c534d3 Mon Sep 17 00:00:00 2001 From: hnu202209100109 <2453373680@qq.com> Date: Tue, 19 Dec 2023 21:28:55 +0800 Subject: [PATCH] ADD file via upload --- 疫情数据爬虫.py | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 疫情数据爬虫.py diff --git a/疫情数据爬虫.py b/疫情数据爬虫.py new file mode 100644 index 0000000..4bd9e18 --- /dev/null +++ b/疫情数据爬虫.py @@ -0,0 +1,60 @@ +# -*- 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') +plt.show() \ No newline at end of file