004 python接口 重定向

 

 

/*
时间:2018/09/18
功能:重定向
目录: 
  一: 查看
  二: 查看历史
     1 方法1 - fiddler
     2 方法2 - python
  三: 禁止 */

 

一: 查看

 

1 : 访问网站: https://www.haoso.com

 

1 : 页面自动跳转: https://www.so.com/

 

1 : 查看访问https://www.haoso.com的状态码是302。

2 : 从服务端返回数据,包含重定向之后的地址。

 

 

二: 查看历史

    1 方法1 - fiddler

1 : 访问url: https://i.cnblogs.com/EditPosts.aspx?opt=1,博客后台编辑地址。

2 : 重定向地址: https://passport.cnblogs.com/user/signin?ReturnUrl=http://i.cnblogs.com/EditPosts.aspx?opt=1&AspxAutoDetectCookieSupport=1 ,博客登录地址。

 

1 : 查看左侧红框内状态码为301和302,可以看到每次重定位的过程。

 

    2 方法2 - python

#coding:utf-8
import requests
import urllib3
urllib3.disable_warnings()

url = "https://i.cnblogs.com/EditPosts.aspx?opt=1"
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36", "Upgrade-Insecure-Requests" : "1", "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding" : "gzip, deflate, br", "Accept-Language" : "zh-CN,zh;q=0.9", } 
# 自动处理重定向 s = requests.session() r1 = s.get(url, headers = header, verify = False) print(r1.status_code) print(r1.url) print(r1.history) for i in r1.history: print(i.url) print(i.status_code)

1 : 使用python,打印每次重定向变化。

 

1 : 可以看到和fiddler,每次重定向地址一样。


三: 禁止

#coding:utf-8
import requests
import urllib3
urllib3.disable_warnings()

url = "https://i.cnblogs.com/EditPosts.aspx?opt=1"
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36",
    "Upgrade-Insecure-Requests" : "1", "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding" : "gzip, deflate, br", "Accept-Language" : "zh-CN,zh;q=0.9", } s = requests.session() r1 = s.post(url, headers = header, allow_redirects = False, # 禁止重定向 verify = False) print(r1.status_code) print(r1.url) print(r1.history) for i in r1.history: print(i.url) print(i.status_code)

1 : 使用python请求,禁止重定向。

 

1 : 可以看到最终url,没有重定向。

 

转载于:https://www.cnblogs.com/huafan/p/9666043.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值