全球疫情数据爬取最新美国数据代码
时间: 2023-06-19 21:06:03 浏览: 83
以下是使用 Python 和 Beautiful Soup 库进行爬取最新美国疫情数据的代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.worldometers.info/coronavirus/country/us/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
data_table = soup.find('table', id='usa_table_countries_today')
table_rows = data_table.tbody.find_all('tr')
for row in table_rows:
columns = row.find_all('td')
state = columns[0].text.strip()
cases = columns[1].text.strip()
new_cases = columns[2].text.strip()
deaths = columns[3].text.strip()
new_deaths = columns[4].text.strip()
active_cases = columns[5].text.strip()
total_cases_per_million = columns[6].text.strip()
deaths_per_million = columns[7].text.strip()
print(state, cases, new_cases, deaths, new_deaths, active_cases, total_cases_per_million, deaths_per_million)
```
这段代码将爬取美国疫情数据网站的 HTML 页面,并使用 Beautiful Soup 库进行解析,然后找到 ID 为 "usa_table_countries_today" 的表格,并遍历表格的每一行数据。对于每一行数据,我们提取每个单元格的文本,并打印出来。你可以根据需要将这些数据保存到文件或数据库中,或者进行其他的数据分析和处理。