如何在Python 3中实现urllib.urlretrieve的等价物?

时间:2017-05-19 14:09:06

标签: python urllib

我想使用其他用户代理从http://www.apkmirror.com下载APK文件。以下适用于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)

我正在努力寻找代码,以便在不使用urllib.request.urlretrieve的情况下在Python 3中执行相同操作,这可能会在将来被弃用。到目前为止,我已经提出了

#!/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)

我怀疑我应该使用write对象的response方法,但我不确定这是怎样的,或者确实是否这样做。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

基于Alternative of urllib.urlretrieve in Python 3.5,我完成了Python 3脚本,如下所示:

#!/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)

答案 1 :(得分:0)

这有点旧,但是下面会有效吗?

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)

答案 2 :(得分:0)

您可以使用shutil.copyfileobj()神奇地将URL字节流复制到文件中。

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)

来源:https://stackoverflow.com/a/48691447/1174102