在重定向后获取网址 [英] Get url after redirect

查看:259
本文介绍了在重定向后获取网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在网页重定向后使用curl或wget获取最终网址。



例如 http://google.com 可能会重定向到 http://www.google.com



内容容易获取(例如 curl --max-redirs 10 http://google.com -L ),但我只对最终的网址感兴趣(在前一种情况下 http://www.google.com )。



有任何方法,只使用linux buildin工具? (仅限命令列)

解决方案

curl -w 选项,子变量 url_effective 是您正在查找的



类似

  curl -Ls -o / dev / null -w %{url_effective} http://google.com 

更多信息

 
-L跟随重定向
-s静默模式。不输出任何内容
-o FILE将输出写入< file>而不是stdout
-w FORMAT完成后输出什么

更多 b
$ b

您可能需要添加 -I (也就是大写的i),这将使命令不下载任何正文,但它也使用HEAD方法,这不是什么问题包括和改变服务器做什么的风险。有时,即使服务器对GET响应正常,服务器也不会对HEAD响应良好。


I need to get the final url after a page redirect preferrably with curl or wget.

For example http://google.com may redirect to http://www.google.com.

The contents are easy to get(ex. curl --max-redirs 10 http://google.com -L), but I'm only interested in the final url (in the former case http://www.google.com).

Is there any way of doing this by using only linux buildin tools? (command line only)

解决方案

curl’s -w option and the sub variable url_effective is what you are looking for.

Something like

curl -Ls -o /dev/null -w %{url_effective} http://google.com

More info

-L         Follow redirects
-s         Silent mode. Don't output anything
-o FILE    Write output to <file> instead of stdout
-w FORMAT  What to output after completion

More

You might want to add -I (that is an uppercase 'i') as well, which will make the command not download any "body", but it then also uses the HEAD method, which is not what the question included and risk changing what the server does. Sometimes servers don't respond well to HEAD even when they respond fine to GET.

这篇关于在重定向后获取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆