图片下载时大小为0字节,但响应为respons.ok和200状态码?

我在同时下载多张图片,但返回的是respons.ok和200状态码,然而下载的文件大小为0字节。我的代码如下:

pic_list=['https://i8.amplience.net/i/nlyscandinavia/146368-0014_01/i-straight-crepe-pant/', 'https://i8.amplience.net/i/nlyscandinavia/146368-0014_02/i-straight-crepe-pant/', 'https://i8.amplience.net/i/nlyscandinavia/146368-0014_04/i-straight-crepe-pant/', 'https://i8.amplience.net/i/nlyscandinavia/146368-0014_05/i-straight-crepe-pant/']for pic_url in pic_list:    url = str(pic_url).replace(' ', '')    print('pic_url : ' + str(url))    folder = full_move_dir + '/' + str(folder_count)    print('after creating folder' + folder)    os.makedirs(folder, exist_ok=True)    try:        pic_ext=str(pic_url.split('.')[-1])        final_pic_ext = pic_ext.split('/')[0]        print(type(final_pic_ext))        print(final_pic_ext)        if final_pic_ext != 'jpeg' and final_pic_ext != 'jpg' and final_pic_ext != 'png':            final_pic_ext = 'jpeg'            print(final_pic_ext)        pic_name = str(pic_count) + '-' + str(file_name_for_folder[-1]) + '.' + str(final_pic_ext)    except Exception as e:        print(e)    with open(os.path.join(folder, pic_name), 'wb') as handle:        if url.find('http') == -1:            url_h = 'http://' + url            try:                headers = {                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}                r = requests.get(url_h, headers=headers)                print(r)                if not r.ok:                    print("NO OK res"+str(r))                else:                    pic_count = pic_count + 1                    handle.write(r)                    sleep(1)            except Exception as e:                print('Invalid URL with http')            finally:                pass        else:            try:                headers = {                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}                r = requests.get(url, headers=headers)                print(r)                if not r.ok:                    print("NO OK res"+str(r))                else:                    pic_count = pic_count + 1                    handle.write(r)                    sleep(1)            except Exception as e:                print('Invalid URL with http')            finally:                pass

我添加了头信息,因为之前遇到权限被拒绝的问题,后来找到了这个解决方案。现在这个方法可以成功下载单张照片,但无法下载多张图片。


回答:

变量 r 是一个 requests对象。图片内容存储在 r.content 中。

这里有一个简单的解决方案来解决你的问题

import requestsimport ospic_list=['https://i8.amplience.net/i/nlyscandinavia/146368-0014_01/i-straight-crepe-pant/', 'https://i8.amplience.net/i/nlyscandinavia/146368-0014_02/i-straight-crepe-pant/', 'https://i8.amplience.net/i/nlyscandinavia/146368-0014_04/i-straight-crepe-pant/', 'https://i8.amplience.net/i/nlyscandinavia/146368-0014_05/i-straight-crepe-pant/']DIR_TO_SAVE = '.'headers = {    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}i=0for pic_url in pic_list:    url = pic_url.strip()    print('pic_url: '+url)    if url[-1] == '/':        filename = url.rstrip('/').split('/')[-1]+str(i)+'.jpeg'        i+=1    else:        filename = url.split('/')[-1]    output = requests.get(url, headers=headers)    if output.status_code == 200:        with open(os.path.join(DIR_TO_SAVE, filename), 'wb') as f:            f.write(output.content)    else:        print("Couldnt get file: "+url)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注