
前言
 最近一直考虑,如何持续的保持我对python学习的热度,思考是否能够尝试任务自动化,简言之,就是有些繁琐的事情利用脚本来实现。今天我讨论学习的任务是如何检测URL重定向到的地址,遗憾的是对https://检测没有实现。实现的原理倒是也简单,就是判断站点返回来的status_code判断是否为3xx,若是,则就简单粗暴地人为是重定向,比如一些突发情况,如访问超时,域名解析错误,网络错误等,则利用try…except…来避免,需要检测的大量的url放入到一个文件中,若站点的格式不统一的话,就做一个简单判断
知识点简单补充
- startswith()方法用于检测字符串是否是以指定子字符串开头
- strip()方法用于移除字符串头尾指定的字符(默认为空格)。
- 使用列表推导式 - x**2 for x in num if x > 0
- [one_ture] if [expression] else [on_false]
- map(lambda x:x**2,filter(lamber x :x>0,num))
- i.strip() for i in open(xxx).readlines() - 脚本代码- import sys 
 import requests
 def check_for_redirects(url):
 #利用allow_redirects=False
 r = requests.get(url,allow_redirects = False ,timeout = 0.5)
 try:- if 300 <= r.status_code <= 400: return r.headers["response"] else: return "[no redirects]"- except requests.exceptions.Timeout: - return "[Timeout]"- except requests.exceptions.ConnectTimeout: - return "[ConnectTimeout]"- except requests.exceptions.SSLError: - return "[SSLError]"- def check_domains(urls): 
 for url in urls:- check_url = url if url.startswith("http") else "http://{}".format(url) redirects_url = check_for_redirects(check_url) print("{}=>{}".format(check_url,redirects_url))- if name == “main“: 
 all_url = “urls.txt”
 try:- all_url = sys.argv[1]- except IndexError: - pass- urls = (i.strip() for i in open(all_url).readlines()) 
 check_domains(urls)
 
总结
最后在多说两句,脚本就定义了两个简单函数,一个是负责结果的返回,一个是文件读取url,有参考别人的地方,具体过程原理一定搞清楚,不然是无从下手去写的,模仿,自己动手,然后才是自己的风格。