Python爬取动物数据
时间: 2023-11-05 12:52:09 浏览: 37
你可以使用 Python 的 requests 和 BeautifulSoup 库来爬取动物数据。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.worldwildlife.org/species/directory?direction=desc&sort=extinction_status"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
animals = []
for animal in soup.find_all("div", class_="species-item"):
name = animal.find("h3", class_="title").text.strip()
status = animal.find("div", class_="status").text.strip()
habitat = animal.find("div", class_="habitat").text.strip()
animals.append({"name": name, "status": status, "habitat": habitat})
print(animals)
```
这个示例代码爬取了世界自然保护联盟(WWF)的动物物种目录页面,提取了动物的名称、灭绝状态和栖息地信息,并将结果以字典的形式添加到一个列表里。你可以根据需要修改代码,以获取你所需的数据。