发布时间:2019-09-10 08:51:09编辑:auto阅读(2081)
楼主在做公司项目的时候遇到url重定向的问题,因此上网简单查找,作出如下结果
由于使用的是语言是python所以以下是python的简单解决方案
http_headers = { 'Accept': '*/*','Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36'}
def get_real_url(url):
    rs = requests.get(url,headers=http_headers,timeout=10)
    rs.url以上代码未有重试机制,下面加上重试机制加以完善
def get_real_url(url,try_count = 1):
if try_count > 3:
    return url
try:
    rs = requests.get(url,headers=http_headers,timeout=10)
    if rs.status_code > 400:
        return get_real_url(url,try_count+1)
    return rs.url
except:
    return get_real_url(url, try_count + 1)
上一篇: ubuntu下python中文编码的设置
下一篇: python绘图示例
 51199
 50619
 41241
 38057
 32519
 29426
 28287
 23145
 23100
 21438
 1495°
 2208°
 1833°
 1763°
 2071°
 1820°
 2505°
 4203°
 4066°
 2907°