python获取文件真实链接的方法,针对于302返回码

yipeiwu_com6年前Python基础

使用模块requests

方式代码如下:

import requests 
url_string="https://******" 
r = requests.head(url_string, stream=True) 
print r.headers['Location'] 

扩展:

设置属性:allow_redirects = True ,则head方式会自动解析重定向链接,requests.get()方法的allow_redirects默认为True,head方法默认为False

url_string="https://******" 
r = requests.head(url_string, stream=True, allow_redirects=True) 
# print r.headers['Location'] 
print r.headers["Content-Length"] 

使用requests.get()方法,该方法会自动解析重定向的链接.

实例:

import requests 
url_string="https://******" 
r = requests.get(url_string, stream=True) 
print r.headers["Content-Length"] 

以上这篇python获取文件真实链接的方法,针对于302返回码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用phoenixdb操作hbase的方法示例

今天看看怎样在 python 中使用 phoenixdb 来操作 hbase 安装 phoenixdb 库 pip install phoenixdb 例子 首先启动 que...

利用Django框架中select_related和prefetch_related函数对数据库查询优化

利用Django框架中select_related和prefetch_related函数对数据库查询优化

实例的背景说明 假定一个个人信息系统,需要记录系统中各个人的故乡、居住地、以及到过的城市。数据库设计如下: Models.py 内容如下:   from django...

判断python字典中key是否存在的两种方法

今天来说一下如何判断字典中是否存在某个key,一般有两种通用做法,下面为大家来分别讲解一下: 第一种方法:使用自带函数实现。 在python的字典的属性方法里面有一个has_key()方...

解决Mac安装scrapy失败的问题

今天打算弄个爬虫,想来想去打算用python弄一个。之前了解到scrapy这个库是个不错的选择,于是开始折腾。可惜第一步就挂了。 安装scrapy库就不成功: Installing...

对python 数据处理中的LabelEncoder 和 OneHotEncoder详解

如下所示: #简单来说 LabelEncoder 是对不连续的数字或者文本进行编号 from sklearn.preprocessing import LabelEncoder le...