python爬虫下载4k图片
时间: 2024-03-28 10:34:42 浏览: 77
爬虫图片下载
4星 · 用户满意度95%
Python爬虫是一种用于自动化获取网页数据的技术,可以用来下载4K图片。以下是一个简单的Python爬虫下载4K图片的示例代码:
```python
import requests
import os
def download_4k_images(url, save_dir):
response = requests.get(url)
if response.status_code == 200:
images = response.json()
for image in images:
image_url = image['url']
image_name = image['name']
save_path = os.path.join(save_dir, image_name)
response = requests.get(image_url)
if response.status_code == 200:
with open(save_path, 'wb') as f:
f.write(response.content)
print(f"Successfully downloaded {image_name}")
else:
print(f"Failed to download {image_name}")
# 示例使用Unsplash API获取4K图片
url = "https://api.unsplash.com/photos/random?query=4k&count=10"
save_dir = "path/to/save/directory"
download_4k_images(url, save_dir)
```
请注意,上述示例代码中使用了Unsplash API来获取4K图片,你需要替换`url`变量为你所需的图片源的API地址。同时,你需要将`save_dir`变量替换为你想要保存图片的目录路径。
阅读全文