본문 바로가기

Dead Code/Python

[파이썬] 게시판에서 파일 이름 규칙에 따라 일괄 다운로드

게시판에서 몇백개의 강의가 올라와 있다고 하자. 해당 동영상 파일을 다운받으려면 게시판을 다 뒤져야 하겠지만, 친절하게 serialize한 규칙으로 첨부 파일이름이 되어있는 게시판이 있다고 하면, 한꺼번에 다운 받을 수 있을 것이다.

 

import os
import requests

for i in range(0, 91):
   
    # file name serial rule
    default_filename = "ET000"
    file_extension = ".mp4"
    file_name = default_filename + str(i) + file_extension
    url = "https://xxx.com/folder/" + file_name
   
    # folder name to save file
    download_path = "C:\\Users\\xxx\\Desktop\\ENG"

    print(file_name)
    print(url)

    try:
        response = requests.get(url, stream=True)
        response.raise_for_status()

        file_path = os.path.join(download_path, file_name)
        with open(file_path, "wb") as file:
            for chunk in response.iter_content(chunk_size=8192):
                file.write(chunk)

        print("File downloaded successfully.")
    except requests.exceptions.RequestException as e:
        print(f"Failed to download the file: {e}")

 

생각대로 되니 참 기쁘다.

이런건 왠만하면, ChatGPT의 도움을 받는게 어떨까?

 

끝.