python爬取两千张哈士奇图片
时间: 2023-10-29 15:07:49 浏览: 116
可以使用Python的requests库和beautifulsoup库来爬取哈士奇图片。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.dogbreedslist.info/all-dog-breeds/Husky.html"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
image_tags = soup.findAll('img')
urls = []
for image in image_tags:
urls.append(image['src'])
count = 0
for url in urls:
if count < 2000:
response = requests.get(url)
with open(f"husky{count}.jpg", "wb") as f:
f.write(response.content)
count += 1
else:
break
```
这个代码将从网站 https://www.dogbreedslist.info/all-dog-breeds/Husky.html 获取所有哈士奇照片的URL,并将前2000张图片下载到当前目录下,文件名分别为 husky0.jpg 到 husky1999.jpg。注意,这个代码只适用于本地爬取,不要滥用爬虫,尊重网站的使用规则。
阅读全文