728x90

파이썬 강의를 듣고 그대로 따라 적어본 코드이다.

경로명은 C:/ 로 하니까 퍼미션 에러가 발생해서 User 폴더로 지정했다.

from urllib import response
import urllib.request as req
from urllib.error import URLError, HTTPError
 
# 다운로드 경로 및 파일명
path_list = ["c:/Users/zx/Documents/Python/test1.jpg""c:/Users/zx/Documents/Python/index.html"]
 
# 다운로드 리소스 url
target_url = ["https://search.pstatic.net/sunny/?src=https%3A%2F%2Fi.pinimg.com%2F736x%2F7a%2F89%2F24%2F7a8924ac188a415140c0c564ec7687c5--plans-lions-club.jpg","https://google.com"]
 
for i, url in enumerate(target_url):
    # 예외 처리
    try:
        # 웹 수신 정보 읽기
        response = req.urlopen(url)
 
        # 수신 내용
        contents = response.read()
 
        print("-----------------------------")
 
        # 상태 정보 중간 출력
        print('Header Info-{} : {}'.format(i, response.info()))
        print('HTTP Status Code: {}'.format(response.getcode()))
        print()
        print("--------------------------------------------")
 
        with open(path_list[i], 'wb'as c:
            c.write(contents)
        
    except HTTPError as e:
        print("Download failed.")
        print("HTTPError code : ", e.code)
    except URLError as e:
        print("Download failed.")
        print("URL Error Reason : ", e.reason)
    # 성공
    else:
        print()
        print("Download Succeed.")
 

 

블로그 이미지

Link2Me

,