0

I would like to download an APK file from http://www.apkmirror.com using a different user agent. The following works in Python 2:

import urllib

class ApkURLopener(urllib.FancyURLopener):
    version = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'

urllib._urlopener = ApkURLopener()

download_link = 'https://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=215041'
download_file = '/tmp/apkmirror_test/youtube.apk'

if __name__ == "__main__":
    urllib.urlretrieve(url=download_link, filename=download_file)

I'm struggling a bit to find the code to do the same in Python 3 without using urllib.request.urlretrieve, which might become deprecated in the future. So far I've come up with

#!/usr/bin/python3
import urllib.request

download_link = 'https://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=215041'
download_file = '/tmp/apkmirror_test/youtube.apk'

USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'

request = urllib.request.Request(url=download_link, headers={'User-Agent': USER_AGENT})

if __name__ == "__main__":
    response = urllib.request.urlopen(url=request)

I suspect I should use the write method of the response object, but I'm not sure how, or indeed whether this is the way to do it. Any advice?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526

3 Answers3

0

Based on Alternative of urllib.urlretrieve in Python 3.5, I completed the Python 3 script as follows:

#!/usr/bin/python3
import urllib.request
import contextlib

download_link = 'https://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=215041'
download_file = '/tmp/apkmirror_test/youtube2.apk'

USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'

request = urllib.request.Request(url=download_link, headers={'User-Agent': USER_AGENT})

if __name__ == "__main__":
    response = urllib.request.urlopen(url=request)

    with open(download_file, 'wb') as out_file:
        with contextlib.closing(response) as fp:
            block_size = 1024 * 8
            while True:
                block = fp.read(block_size)
                if not block:
                    break
                out_file.write(block)
Community
  • 1
  • 1
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
0

This is a bit old, but would the following work?

import urllib.request as rq, os
from urllib.parse import urlparse


url = "your_url"
result = urllib.request.urlopen(url)
result_url = result.url
result_url_parse = urlparse(result_url)
result_path = result_url_parse.path
filename = os.path.basename(result_path)
download = rq.urlretrieve(url,filename)
caydin
  • 161
  • 1
  • 3
0

You can use shutil.copyfileobj() to magically copy from the url bytestream to the file.

import urllib.request
import shutil
    
url = "https://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=215041"    
output_file = "/tmp/apkmirror_test/youtube.apk"
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'

with urllib.request.urlopen(url, headers={'User-Agent': user_agent}) as response, open(output_file, 'wb') as out_file:
  shutil.copyfileobj(response, out_file)

Source: https://stackoverflow.com/a/48691447/1174102

Michael Altfield
  • 2,083
  • 23
  • 39