Openai/692a4e61-621c-8011-95ff-af7aaecbf96d

Revision as of 05:28, 29 November 2025 by Lukegao (talk | contribs) (by freem.ai...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


This conversation was exported from ChatGPT[1]

gpt-5-mini edit

User: https://imgur.com/の画像をダウンロードしたい まずURLリストを用意→画像表示可能か判定→表示可能な画像のみダウンロードしたい edit

https://imgur.com/の画像をダウンロードしたい まずURLリストを用意→画像表示可能か判定→表示可能な画像のみダウンロードしたい

Assistant: なるほど、やりたいことは以下の手順ですね: edit

なるほど、やりたいことは以下の手順ですね:

  1. 画像URLリストを用意
  2. そのURLが実際に画像としてアクセス可能かチェック
  3. アクセス可能な画像のみダウンロード

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

  1. "画像ダウンロード方法". ChatGPT. Retrieved 2025-11-29.