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.

69 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: utf-8 -*-
"""
Created on Thu May 12 17:29:46 2022
@author: 188
"""
import requests
# 请求的url
url = "https://www.bitpush.news/covid19/"
# 设置请求头信息
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 SE 2.X MetaSr 1.0"
}
# 使用reqeusts模快发起 GET 请求
response = requests.get(url, headers=headers)
# 获取请求的返回结果
html = response.text
# 导入 lxml
from lxml import etree
# 创建一个 lxml 对象,编码方式设为 utf-8
parse = etree.HTMLParser(encoding='utf-8') # 添加编码
# 解析 requests 返回的响应结果
doc = etree.HTML(html)
# 美国各地区
area = doc.xpath('//div[@class="table_container"]//tbody/tr/td/span/text()')
# 确诊人数
person = doc.xpath('//div[@class="table_container"]//tbody/tr/td[2]/text()')
# 由于确诊人数中有逗号,我们使用列表推导式删除
person = [x.replace(",", "") for x in person]
# 死亡人数
death = doc.xpath('//div[@class="table_container"]//tbody/tr/td[3]/text()')
# 同样使用列表推导式删除逗号
death = [x.replace(",", "") for x in death]
# 打包数据之后将其转换成列表
message = list(zip(area, person, death))
# 导入python中的内置模块csv
import csv
with open("content.csv", "w") as f:
w = csv.writer(f)
w.writerows(message)
import pandas as pd
# 读取数据
df = pd.read_csv("content.csv", names=["area", "person", "death"], encoding='gbk')
#前101都不是美国境内数据所以直接截取后面58个州
df1 = df.drop(0).tail(58).head(15)
import matplotlib.pyplot as plt
# 在jupyter中直接展示图像
%matplotlib inline
# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['figure.figsize'] = (10, 5) # 设置figure_size尺寸
# x轴坐标
x = df1["area"].values
# y轴坐标
y = df1["person"].values
# 绘制柱状图
plt.bar(x, y)
# 设置x轴名称
plt.xlabel("美国境内地区",fontsize=14)
# 设置x轴名称
plt.ylabel("确诊人数",fontsize=14)
plt.show()