python自动解析301、302重定向链接

使用模块requests

方式代码如下:

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

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

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
url_string="http://******"  
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"]  
posted @   I'm_江河湖海  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示