写一段爬取新冠疫情数据的代码
时间: 2023-10-05 08:07:11 浏览: 139
以下是一个简单的Python代码示例,用于爬取新冠疫情数据:
```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')
# 解析数据
script = soup.find(id='getListByCountryTypeService2true')
text = script.string
text = text.replace('try { window.getListByCountryTypeService2true = ', '')[:-1]
data = eval(text)
# 输出结果
for item in data:
print(item['provinceName'], item['confirmedCount'], item['deadCount'], item['curedCount'])
```
这段代码使用requests库发送请求,然后使用BeautifulSoup库解析HTML页面。在解析完成后,我们可以使用eval()函数将包含疫情数据的JavaScript代码转换为Python对象,并输出结果。请注意,这只是一个简单的示例代码,实际爬取数据时需要注意网站的反爬措施和法律法规。
相关问题
利用python写一段爬取新冠疫情数据的代码
好的,以下是一个简单的Python爬取新冠疫情数据的代码,使用了requests库和BeautifulSoup库:
``` python
import requests
from bs4 import BeautifulSoup
url = "https://covid19.who.int/region/wpro/country/cn"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
confirmed_cases = soup.find("div", {"class": "sc-fMiknA cYomRb"}).find("div", {"class": "sc-fMiknA jFfjLH"}).text
deaths = soup.find("div", {"class": "sc-fMiknA bHeGOS"}).find("div", {"class": "sc-fMiknA jFfjLH"}).text
recovered = soup.find("div", {"class": "sc-fMiknA eRBoJf"}).find("div", {"class": "sc-fMiknA jFfjLH"}).text
print("Confirmed cases: " + confirmed_cases)
print("Deaths: " + deaths)
print("Recovered: " + recovered)
```
这个代码使用了WHO的网站来获取中国的新冠疫情数据,首先使用requests库获得网页内容,然后使用BeautifulSoup库来解析网页内容并查找相应的数据。最后将数据打印出来。你可以根据需要修改代码来爬取其他网站上的数据。
利用python爬取新冠疫情数据
可以使用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字典,打印出疫情数据。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)