Download File Từ URL Trong Python

Download File Từ URL Trong Python

Cách 1: dùng module Requests

  • Bước 1: pip install requests
  • Bước 2:
    # 1. Import the requests library
    import requests
    URL = “https://instagram.com/favicon.ico”
    # 2. download the data behind the URL
    response = requests.get(URL)
    # 3. Open the response into a new file called instagram.ico
    open(“instagram.ico”, “wb”).write(response.content)

Cách 2: dùng module Wget

  • Bước 1: pip install wget
  • Bước 2:
    import wget
    URL = “https://instagram.com/favicon.ico”
    response = wget.download(URL, “instagram.ico”)

Cách 3: dùng module URLLIB

  • Bước 1: pip install urllib
  • Bước 2:
    from urllib import request
    URL = “https://instagram.com/favicon.ico”
    response = request.urlretrieve(“https://instagram.com/favicon.ico”, “instagram.ico”)

 

Bài viết có tham khảo: cảm ơn tác giả!