/* 时间: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,没有重定向。