parent
5dee333e14
commit
90c98b72bb
@ -0,0 +1,43 @@
|
|||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
plt.rcParams["font.sans-serif"]=["SimHei"] # 指定画图的时候可以显示中文
|
||||||
|
plt.rcParams['axes.unicode_minus']=False # 指定画图的时候可以有负号
|
||||||
|
|
||||||
|
url = 'https://www.bitpush.news/covid19/' # 链接
|
||||||
|
response = requests.get(url) # 发送get请求 拿到请求体
|
||||||
|
response = response.content # 拿到返回的文本内容
|
||||||
|
soup = BeautifulSoup(response, 'html.parser') # 使用BeautifulSoup库来解析html文件
|
||||||
|
allinfo = soup.find_all('div', class_='card-inner-wrapper') # 找到美国疫情数据相关的坐标 拿到信息
|
||||||
|
mymap = {} # 声明一个哈希表 记录前15确诊人数有关的信息
|
||||||
|
for info in allinfo:
|
||||||
|
findh1 = info.find('h1', string='美国境内统计数据') # 判断 如果是美国疫情数据才做信息处理
|
||||||
|
if findh1 is not None: # 如果找到了 也就是findh1不为空
|
||||||
|
allstate = info.find_all("tr") # 找到所有的行信息 也就是确诊信息
|
||||||
|
for state in allstate: # 遍历所有的行信息 拿到州名字以及确诊信息
|
||||||
|
statename = state.find('td', class_='table_card_cell_col_0 table_card_cell_stringwithicon_type')
|
||||||
|
if statename is not None: # 如果州信息不为空才能拿到
|
||||||
|
statename = statename.text
|
||||||
|
diagnose = state.find('td', class_='table_card_cell_col_1 table_card_cell_int_type')
|
||||||
|
if diagnose is not None: # 如果确诊信息不为空才能拿到
|
||||||
|
diagnose = diagnose.text
|
||||||
|
diagnose = int(''.join(diagnose.split(','))) # 这是因为国外的人数会有, 比如1000是1,000所以要特殊处理一下转成1000
|
||||||
|
die = state.find('td', class_='table_card_cell_col_2 table_card_cell_float_type')
|
||||||
|
if die is not None:
|
||||||
|
die = die.text
|
||||||
|
die = int(''.join(die.split(','))) # 同上
|
||||||
|
if diagnose is not None:
|
||||||
|
mymap[statename] = diagnose # 如果不为空的话 将 [州、确诊信息] 放在里面
|
||||||
|
X, Y = [], [] # X轴 Y轴
|
||||||
|
for k, v in sorted(mymap.items(), key=lambda x: -x[1])[:15]: # 选取确诊信息的前15条
|
||||||
|
X.append(k)
|
||||||
|
Y.append(v) # 添加信息
|
||||||
|
plt.bar(X, Y, color="blue") # 画柱状图 指定颜色为蓝色
|
||||||
|
plt.xticks(rotation=45) # 为了避免X轴过于挤看不清 将它旋转45度
|
||||||
|
plt.xlabel("州")
|
||||||
|
plt.ylabel("确诊人数")
|
||||||
|
plt.title("确诊人数前15")
|
||||||
|
plt.savefig('bar.jpg')
|
||||||
|
plt.show()
|
Loading…
Reference in new issue