从字典中的多个 URLS 下载多个图像 python

Download multiple images from multiple URLS in dictionary python

我正在尝试编写一个函数,从 dynamodb 获取图像的 URL,并使用 url 将它们下载到我机器的文件夹中。

下面是我的代码:-

#reference dynamodb
dynamodb = boto3.resource('dynamodb')
#point at music table
table = dynamodb.Table('Music')
#get image url values from dynamodb
imageUrl = table.scan(AttributesToGet=['img_url'])
#add urls found into dict
urls=[]
urls.append(imageUrl)

#print(urls)

#download all images from their urls
for url in urls:
    url = imageUrl['Items']
    image = url[0]['img_url']
    file_name = image.split('/')[-1]
    response = requests.get(image)
    file = open("/Users/abdotech/Desktop/images/"+file_name, "wb")
    for chunk in response:
      file.write(chunk)


    file.close()

此代码仅从第一个 url 下载一个图像,尽管我正在遍历包含多个图像 url 的字典列表。 print(urls) returns 在数据库中找到的所有图像 urls,所以这就是我如何知道我的字典有所有需要的 urls.

所以基本上我的错误在于 for 语句,我无法弄清楚可能导致问题的原因。

提前致谢。

编辑:

for url in imageUrl:
    url = imageUrl['Items']
    image = url['img_url']
    file_name = image.split('/')[-1]
    response = requests.get(image)
    file = open("/Users/abdotech/Desktop/images/"+file_name, "wb")
    for chunk in response:
      file.write(chunk)


    file.close()

错误:TypeError:列表索引必须是整数或切片,而不是 str

这里有一些问题。 error:TypeError: list indices must be integers or slices, not str 告诉您您正在尝试使用字符串访问列表项。我认为您的变量名在这里也没有帮助您。

基本上,您的 url 变量被设置为 dynamodb 响应中的字典列表,并且您忽略了在迭代结果集时尝试设置的 url 变量。

此外,由于 urls.append(imageUrl),您的 for url in urls 将循环一次,因为 imageUrl 这里实际上是发电机响应指令。

这样的事情应该有所帮助。

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Music')
scan_response = table.scan(AttributesToGet=['img_url'])

for record in scan_response["Items"]:
    img_url = record["img_url"]
    file_name = img_url.split('/')[-1]
    response = requests.get(img_url)
    with open("/Users/abdotech/Desktop/images/" + file_name, "wb") as file:
        for chunk in response:
          file.write(chunk)

P.S。尽可能使用 with 语句。

您没有正确迭代结果。扫描的响应包含您需要迭代的项目列表,您不需要为此创建另一个 urls 列表。您还在循环中对 0 索引进行硬编码,因此它始终抓取列表的第一项而不是循环中的当前项。

尝试这样的事情:

#reference dynamodb
dynamodb = boto3.resource('dynamodb')
#point at music table
table = dynamodb.Table('Music')
#get image url values from dynamodb
imageUrl = table.scan(AttributesToGet=['img_url'])

#print(urls)

#loop through all items returned by dynamodb
for url in imageUrl['Items']:
    image = url['img_url']
    file_name = image.split('/')[-1]
    response = requests.get(image)
    file = open("/Users/abdotech/Desktop/images/"+file_name, "wb")
    for chunk in response:
      file.write(chunk)


    file.close()