
エクセルのシートがあります。
各行が商品番号で、縦の列はimageという画像のURLが記載された列です。
商品によって画像があったりなかったりするので、
image列は「urlがある場合」と「None」の場合の2通りです。
今、このimage列を取得して、
for文で「画像があればそのURLからDLする、"None"であればprintで"その商品番号は画像がない」という分を出力する処理を作成しています。
★★★の部分です。
現状、画像がないケースでは「Noneの画像 "None"」と出力されてしまうので、それを「'item["image_aaa"]'の画像"None"」と出力できるうようにするにはどうすればよいか?という質問です。
#画像ダウンロード
def dl_images(images, sn, dir_title, footer):
def dl_images( images):
#Noneを判定 if images == "None": print(f'{images}の画像 "None"') #★★★ else: if type(images) == list: arr_images = images.split(',') for image in arr_images: pass # print(image) else: arr_images = images.split(',') for image in arr_images: num = str(arr_images.index(image)+1).zfill(2) file_name = f'{dir_title}/{sn[1]}_{footer}_{num}.jpg' #DL済ならスキップ if not os.path.isfile(file_name): response = requests.get(image) res_image = response.content with open(file_name, "wb") as fn: fn.write(res_image) #画像のリサイズを行う NFN.resize_images(dir_title) else: pass # print("画像はDL済み") # print(f'{sn[1]}:画像DL&リサイズ完了')
dl_images(item["image_aaa"], item["sn"], dir_title, "aaa")












回答3件
あなたの回答
tips
プレビュー