python爬虫美国疫情
时间: 2023-12-02 08:05:40 浏览: 121
python爬虫获取疫情各地区确诊人数,实现数据可视化
以下是使用Python爬虫获取美国疫情数据的步骤:
1. 导入必要的库
```python
import requests
from bs4 import BeautifulSoup
```
2. 发送请求并获取HTML页面
```python
url = 'https://www.worldometers.info/coronavirus/country/us/'
response = requests.get(url)
html = response.text
```
3. 解析HTML页面并获取所需数据
```python
soup = BeautifulSoup(html, 'html.parser')
cases = soup.find_all('div', class_='maincounter-number')
total_cases = cases[0].text.strip()
total_deaths = cases[1].text.strip()
total_recovered = cases[2].text.strip()
```
4. 输出所需数据
```python
print('Total cases:', total_cases)
print('Total deaths:', total_deaths)
print('Total recovered:', total_recovered)
```
阅读全文