ダウンロード

記事
IT・テクノロジー

Import

import requests
from requests.exceptions import RequestException

ダウンロード

 シンプル
response = requests.get(URL)
if response.status_code == 200:
    with open(パス, 'wb') as fw:
        for chunk in response.iter_content(chunk_size=100000):
            fw.write(chunk)

    print(f'{URL} のダウンロードが成功しました')
else:
    print(f'{URL} のダウンロードに失敗しました (ステータスコード: {response.status_code})')
 例外処理
try:
    response = requests.get(URL, timeout=10)
    response.raise_for_status()
    with open(パス, 'wb') as fw:
        for chunk in response.iter_content(chunk_size=100000):
            fw.write(chunk)

    print(f'{URL} のダウンロードが成功しました')
except RequestException as e:
    print(f'{URL} のダウンロード中にエラーが発生しました: {e}')

サービス数40万件のスキルマーケット、あなたにぴったりのサービスを探す ココナラコンテンツマーケット ノウハウ記事・テンプレート・デザイン素材はこちら