如何从重定向的URL下载文件?

时间:2017-06-19 10:55:48

标签: python python-3.x python-requests urllib2

我需要使用网址下载文件 - > https://readthedocs.org/projects/django/downloads/pdf/latest/

此网址重定向到带有.pdf文件的网址。

如何使用python使用此URL下载该文件?

我试过了: -

import urllib
def download_file(download_url):
    web_file = urllib.urlopen(download_url)
    local_file = open('some_file.pdf', 'w')
    local_file.write(web_file.read())
    web_file.close()
    local_file.close()

if __name__ == 'main':
    download_file('https://readthedocs.org/projects/django/downloads/pdf/latest/')

但这不起作用

2 个答案:

答案 0 :(得分:4)

import requests
url = 'https://readthedocs.org/projects/django/downloads/pdf/latest/'
r = requests.get(url, allow_redirects=True)  # to get content after redirection
pdf_url = r.url # 'https://media.readthedocs.org/pdf/django/latest/django.pdf'
with open('file_name.pdf', 'wb') as f:
    f.write(r.content)

如果您想从其他方法下载该文件,或者您只想获得最终重定向的网址,可以使用requests.head(),如下所示:

r = requests.head(url, allow_redirects=True)  # to get only final redirect url

答案 1 :(得分:-1)

这将有效:

>>> import urllib
>>> urllib.urlretrieve('https://readthedocs.org/projects/django/downloads/pdf/latest/', 'filename.pdf')