python 重定向获取真实url的方法
楼主在做公司项目的时候遇到url重定向的问题,因此上网简单查找,作出如下结果
由于使用的是语言是python所以以下是python的简单解决方案
http_headers={'Accept':'*/*','Connection':'keep-alive','User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.116Safari/537.36'} defget_real_url(url): rs=requests.get(url,headers=http_headers,timeout=10) rs.url
以上代码未有重试机制,下面加上重试机制加以完善
defget_real_url(url,try_count=1): iftry_count>3: returnurl try: rs=requests.get(url,headers=http_headers,timeout=10) ifrs.status_code>400: returnget_real_url(url,try_count+1) returnrs.url except: returnget_real_url(url,try_count+1)
以上这篇python重定向获取真实url的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。