Update 疫情数据爬虫.py

main
hnu202209100109 11 months ago
parent e0b9e1a1da
commit fb254c4144

@ -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()
Loading…
Cancel
Save