利用python爬取新冠疫情数据
时间: 2023-10-05 18:07:11 浏览: 122
可以使用Python的Requests和BeautifulSoup库来爬取新冠疫情数据。以下是一个简单的示例:
```python
import requests
from bs4 import BeautifulSoup
url = "https://ncov.dxy.cn/ncovh5/view/pneumonia"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 获取国内疫情数据
china_data = soup.find("script", attrs={"id": "getAreaStat"}).string
china_data = china_data.replace("try { window.getAreaStat =", "")[:-1]
china_data = json.loads(china_data)
# 获取全球疫情数据
global_data = soup.find("script", attrs={"id": "getListByCountryTypeService2"}).string
global_data = global_data.replace("try { window.getListByCountryTypeService2true = ", "")[:-1]
global_data = json.loads(global_data)
# 打印疫情数据
print("国内疫情数据:", china_data)
print("全球疫情数据:", global_data)
```
上述代码首先使用Requests库发送GET请求,获取新冠疫情数据页面的HTML文档。然后使用BeautifulSoup库解析HTML文档,从中提取出国内和全球疫情数据的JSON字符串。最后将JSON字符串解析成Python字典,打印出疫情数据。
阅读全文