게시판에서 몇백개의 강의가 올라와 있다고 하자. 해당 동영상 파일을 다운받으려면 게시판을 다 뒤져야 하겠지만, 친절하게 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
# 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의 도움을 받는게 어떨까?
끝.
'Dead Code > Python' 카테고리의 다른 글
[파이썬] 확장자별로 폴더 생성, 파일 이동 (2) (0) | 2024.04.01 |
---|---|
파이썬3.11.5, 포터블로 환경 설정하기 (0) | 2023.09.06 |
티스토리 블로그 포스팅 제목 목록 txt파일 만들기 (1) | 2022.11.03 |
[파이썬] fatal error in launcher : unable to create process using... (0) | 2022.01.18 |
[파이썬] 확장자별로 폴더 생성, 파일 이동 (1) (0) | 2020.12.23 |
[파이썬] 초보자들의 단골메뉴 - while로 구구단 짜기 (0) | 2017.07.19 |
[파이썬] if 구문 연습, 가위바위보 짜기 (0) | 2017.07.17 |
[파이썬] 파이썬을 처음 공부하는 사람들을 위한 무료 eBOOK (0) | 2017.07.14 |