📜  Python——在全球范围内获得确诊、康复、死亡病例

📅  最后修改于: 2022-05-13 01:55:32.596000             🧑  作者: Mango

Python——在全球范围内获得确诊、康复、死亡病例

在本文中,我们将了解如何创建一个Python脚本,该脚本讲述世界各地的电晕病例,即确诊病例数、患者康复病例数和因电晕而导致的总死亡人数。

所需模块和安装:

Requests : Requests 允许您非常轻松地发送 HTTP/1.1 请求。无需手动将查询字符串添加到您的 URL。

pip install requests

Beautiful Soup: Beautiful Soup 是一个库,可以轻松地从网页中抓取信息。它位于 HTML 或 XML 解析器之上,提供用于迭代、搜索和修改解析树的 Pythonic 习惯用法。

pip install beautifulsoup4

解释 -
我们将在requests BeautifulSoup的帮助下从网站上抓取数据,然后将原始数据转换为 html 代码,然后对其进行过滤以获得所需的输出,并将信息保存在字典中。

下面是实现——

Python3
# importing libraries
from bs4 import BeautifulSoup as BS
import requests
  
  
# method to get the info
def get_info(url):
      
    # getting the request from url 
    data = requests.get(url)
  
    # converting the text 
    soup = BS(data.text, 'html.parser')
      
    # finding meta info for total cases
    total = soup.find("div", class_ = "maincounter-number").text
      
    # filtering it
    total = total[1 : len(total) - 2]
      
    # finding meta info for other numbers
    other = soup.find_all("span", class_ = "number-table")
      
    # getting recovered cases number
    recovered = other[2].text
      
    # getting death cases number
    deaths = other[3].text
      
    # filtering the data
    deaths = deaths[1:]
      
    # saving details in dictionary
    ans ={'Total Cases' : total, 'Recovered Cases' : recovered, 
                                 'Total Deaths' : deaths}
      
    # returning the dictionary
    return ans
   
# url of the corona virus cases
url = "https://www.worldometers.info/coronavirus/"
  
# calling the get_info method
ans = get_info(url)
  
# printing the ans
for i, j in ans.items():
    print(i + " : " + j)


输出 :

Total Cases : 2,129,927
Recovered Cases : 539,063
Total Deaths : 142,716