如何从重定向的 URL 下载文件? [英] How to download a file from a URL which redirects?

查看:45
本文介绍了如何从重定向的 URL 下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to download a file using url-->https://readthedocs.org/projects/django/downloads/pdf/latest/

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

This url redirects to a url with a .pdf file.

如何使用 python 下载带有此 url 的文件?

How can I download that file with this url using python ?

我试过了:-

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/')

但这不起作用

推荐答案

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)

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

If you want to download the file from other method or you want to get only final redirected URL you can use requests.head() as shown below:

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

这篇关于如何从重定向的 URL 下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆