Home > Software design >  Download .img file from given link using wget-module
Download .img file from given link using wget-module

Time:02-07

I am trying to download a file in python using the wget module. The problem is, that the original file size is about 125mb, the file size of my download is just ~7kb

My code:

import wget
download_link = "https://dl.twrp.me/gauguin/twrp-3.5.2_10-0-gauguin.img"
wget.download(download_link)

Output:

100% [................................................................................] 6846 / 6846'twrp-3.5.2_10-0-gauguin.img'

File size:

francesco@francesco-ubuntu:~/Dokumente/GIT/python_adb$ ls -al twrp-3.5.2_10-0-gauguin.img 
-rw-rw-r-- 1 francesco francesco 6846 Feb  6 22:26 twrp-3.5.2_10-0-gauguin.img

Am I doing something wrong? Like I said, the original file size is ca 125mb, the file I downloaded around 7kb

Thanks in advance!

CodePudding user response:

To correctly download the file, try to set Referer and User-Agent HTTP header (example with requests module):

import requests


url = "https://dl.twrp.me/gauguin/twrp-3.5.2_10-0-gauguin.img"

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0",
    "Referer": "https://dl.twrp.me/gauguin/twrp-3.5.2_10-0-gauguin.img",
}

r = requests.get(url, headers=headers)
with open("twrp-3.5.2_10-0-gauguin.img", "wb") as f_out:
    f_out.write(r.content)

Download the file:

-rw-r--r-- 1 root root 134217728 feb  6 22:49 twrp-3.5.2_10-0-gauguin.img

CodePudding user response:

So i found the solution but using request library, but I think you will be able to fix your code.

The fix is easy, you just need to specify Referer header

Referer: https://dl.twrp.me/gauguin/twrp-3.5.2_10-0-gauguin.img

So, my code with requests

import requests

image_url = "https://dl.twrp.me/gauguin/twrp-3.5.2_10-0-gauguin.img"

headers = {
    "Referer": "https://dl.twrp.me/gauguin/twrp-3.5.2_10-0-gauguin.img",
}

img_data = requests.get(image_url, headers=headers).content
with open('image_name.img', 'wb') as handler:
    handler.write(img_data)
  •  Tags:  
  • Related