首页 > 解决方案 > 无法从 Unsplash 网站下载图像

问题描述

各位晚上好

所以我试图从 unsplash 下载所有壁纸图片,从 Firefox 的网络选项卡中获取他们的 API 链接。(它每页只加载 30 个链接,这很好)

编辑:现在我得到了 30 张相同的图像,但我的文件夹中有不同的名称。任何人都可以帮忙:)

这是网络选项卡中的 API 链接:API 网络选项卡链接

这是我的代码:

import json
import pandas as pd
import requests
import os

with open ("Unsplash.json",encoding="UTF-8") as f:
 s = json.load(f)
    

U = pd.json_normalize(s)


U.rename(columns={'alt_description':'Name','urls.raw':'Links'},inplace=True)
Links = U["Links"].tolist()
Name = U["Name"].tolist()

# 1. Works Fine

for Index,l in enumerate(Links):
 data = requests.get(l).content
 with open ("Unsplash_Pics//" + str(Index) + ".jpg","wb") as f:
  f.write(data) 

# 2. i get Same image 29 times but with different names :(

for l in Links:
 data = requests.get(l).content
 for name in Name:
  with open ("Unsplash_Pics//" + str(name) + ".jpg","wb") as f:
   f.write(data)
     

标签: pythonpython-3.ximageweb-scrapingdownload

解决方案


你试过这个来解决命名问题吗?

for Index,l in enumerate(Links):
    data = requests.get(l).content
    with open ("Unsplash_Pics//" + str(name[Index]) + ".jpg","wb") as f:
        f.write(data) 

推荐阅读