【问题标题】:python how to get the final destination after redirectionspython如何在重定向后获得最终目的地
【发布时间】:2020-05-10 14:35:34
【问题描述】:

我正在分析 url 网络钓鱼数据,一些 url 可能有多个重定向(301、302)。

我可以使用curl获得最终目的地和重定向次数:

curl -Ls -o /dev/null -w "%{num_redirects},%{url_effective}" <url>

用pythonrequests做同样的事情:

import requests

r = requests.get(url, allow_redirects=True)

if r.history:
  print(f'{len(r.history)},{r.history[-1].url}')

我发现使用requests history 并没有给我最终的目的地(尽管下载的内容与curl 相同)。

例如,给定 url(这是一个合法的 url,我发誓)https://ludik.xyz/music,这就是我用curl 得到的:

1,https://ludik.herokuapp.com/#/

这是我在 python 中得到的:

1,https://ludik.xyz/music

如何在 python 中进行所有重定向后获得最终目的地?

【问题讨论】:

    标签: python curl redirect python-requests


    【解决方案1】:

    在响应对象上设置最终的 url:

    In [5]: import requests 
       ...:  
       ...: r = requests.get("https://ludik.xyz/music")                                                                                                                                                                                           
    
    In [8]: r.url                                                                                                                                                                                                                                 
    Out[8]: 'https://ludik.herokuapp.com/#/'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2015-09-08
      • 2021-02-15
      • 2016-05-04
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多