我需要在页面重定向后获得最终的URL,最好是使用curl或wget。
例如,可能会重定向到。
内容很容易获得(例如:curl --max-redirs 10 http://google.com -L
),但我只对最后的url感兴趣(在前面的例子中是http://www.google.com)。
有没有办法只使用Linux内置的工具来做到这一点?(仅限命令行)
发布于 2010-06-19 12:15:37
作为另一种选择:
$ curl -i http://google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sat, 19 Jun 2010 04:15:10 GMT
Expires: Mon, 19 Jul 2010 04:15:10 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
但它不会超过第一个。
发布于 2010-06-19 13:07:31
谢谢。我最终实现了您的建议: curl -i + grep
curl -i http://google.com -L | egrep -A 10 '301 Moved Permanently|302 Found' | grep 'Location' | awk -F': ' '{print $2}' | tail -1
如果网站没有重定向,则返回空白,但这对我来说已经足够了,因为它在连续的重定向上工作。
可能有but,但乍一看它工作得很好。
发布于 2012-09-17 01:07:51
通常你可以用wget来做这件事。wget --content-disposition
"url“另外,如果你添加了url,你实际上不会保存这个文件。
wget -O /dev/null --content-disposition example.com
https://stackoverflow.com/questions/3074288
复制相似问题