You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.8 KiB
49 lines
1.8 KiB
import requests
|
|
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
|
|
# 导入 lxml
|
|
from lxml import etree
|
|
parse = etree.HTMLParser(encoding='utf-8') # 添加编码
|
|
# 解析 requests 返回的响应结果
|
|
doc = etree.HTML(html)
|
|
# 州名
|
|
country = doc.xpath('//*[@id="main"]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td/span/text()')
|
|
# 确诊人数
|
|
person = doc.xpath('//*[@id="main"]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td[2]/text()')
|
|
# 由于确诊人数中有逗号,我们使用列表推导式删除
|
|
person = [x.replace(",", "") for x in person]
|
|
# 死亡人数
|
|
death = doc.xpath('//*[@id="main"]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td[3]/text()')
|
|
# 同样使用列表推导式删除逗号
|
|
death = [x.replace(",", "") for x in death]
|
|
# 打包数据之后将其转换成列表
|
|
message = list(zip(country, person, death))
|
|
message=message[:10]
|
|
# 导入python中的内置模块csv
|
|
import csv
|
|
with open("content.csv", "w",encoding="utf-8") as f:
|
|
w = csv.writer(f)
|
|
w.writerows(message)
|
|
import pandas as pd
|
|
|
|
# 读取数据
|
|
df = pd.read_csv("content.csv", names=["country", "person", "death"],encoding="utf-8")
|
|
print(df)
|
|
import matplotlib.pyplot as plt
|
|
plt.rcParams['font.sans-serif'] = ['SimHei']
|
|
plt.rcParams['axes.unicode_minus'] = False
|
|
plt.rcParams['figure.figsize'] = (10, 5)
|
|
x = df["country"]
|
|
y = df["death"]
|
|
plt.bar(x,y)
|
|
plt.xlabel("美国州名",fontsize=14)
|
|
plt.ylabel("死亡人数",fontsize=14)
|
|
plt.show()
|
|
|