Openai/692a4e61-621c-8011-95ff-af7aaecbf96d
Appearance
This conversation was exported from ChatGPT[1]
gpt-5-mini[edit | edit source]
User: https://imgur.com/の画像をダウンロードしたい まずURLリストを用意→画像表示可能か判定→表示可能な画像のみダウンロードしたい[edit | edit source]
https://imgur.com/の画像をダウンロードしたい まずURLリストを用意→画像表示可能か判定→表示可能な画像のみダウンロードしたい
Assistant: なるほど、やりたいことは以下の手順ですね:[edit | edit source]
なるほど、やりたいことは以下の手順ですね:
- 画像URLリストを用意
- そのURLが実際に画像としてアクセス可能かチェック
- アクセス可能な画像のみダウンロード
Python でシンプルに実装できます。requests と os を使った例を示します。
import requests
import os
=== 画像URLリスト ===
urls = [
"https://i.imgur.com/XXXXXXX.jpg",
"https://i.imgur.com/YYYYYYY.png",
]
=== 保存先フォルダ ===
save_folder = "downloaded_images"
os.makedirs(save_folder, exist_ok=True)
for url in urls:
try:
# HEADリクエストでアクセス可能かチェック
response = requests.head(url, allow_redirects=True)
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
# GETリクエストで画像を取得
img_data = requests.get(url).content
filename = os.path.join(save_folder, os.path.basename(url))
with open(filename, "wb") as f:
f.write(img_data)
print(f"Downloaded: {filename}")
else:
print(f"Not an image or inaccessible: {url}")
except Exception as e:
print(f"Error accessing {url}: {e}")
ポイント:
- HEAD で事前チェックして画像かどうか判定
- ステータスコード 200 かつ Content-Type に "image" が含まれているか確認
- os.path.basename(url) でファイル名を取得
もし希望なら、大量URLでも効率的に並列ダウンロードできるバージョンも作れます。作ってほしいですか?
References[edit | edit source]
- ↑ "画像ダウンロード方法". ChatGPT. Retrieved 2025-11-29.