[英]wget: don't follow redirects
如何防止wget跟踪重定向?
--max-redirect 0
我没试过这个,它要么不允许,要么允许无限......
使用curl
而不使用-L
而不是wget
。 使用curl
时省略该选项可防止重定向被跟踪。
如果您使用curl -I <URL>
那么您将获得标题而不是重定向HTML。
如果您使用curl -IL <URL>
那么您将获得URL的标题,以及您重定向到的URL的标题。
wget默认情况下最多可以跟踪20个重定向。 但是,它不会跨越主机。 如果您要求wget下载example.com
,它将不会触及www.example.com
任何资源。 wget会将此检测为跨越另一台主机的请求并做出决定。
简而言之,您应该执行:
wget --mirror www.example.com
而不是
wget --mirror example.com
现在让我们假设www.example.com
的所有者在example.com
有几个子域名,我们对它们都感兴趣。 如何进行?
试试这个:
wget --mirror --domains=example.com example.com
wget现在将访问example.com的所有子域,包括m.example.com
和www.example.com
。
某些版本的wget
具有--max-redirect
选项:请参见此处
通常,依赖于特定数量的重定向并不是一个好主意。
例如,为了下载IntellijIdea,承诺始终解析为最新版本的Community Edition for Linux的URL类似于https://download.jetbrains.com/product?code=IIC&latest&distribution=linux
,但是如果你现在访问该URL,在到达实际可下载文件之前,您将被重定向两次(2次)。 在将来,您可能会被重定向三次,或者根本不被重定向。
解决此问题的方法是使用HTTP HEAD动词。 以下是我在IntellijIdea中解决它的方法:
# This is the starting URL.
URL="https://download.jetbrains.com/product?code=IIC&latest&distribution=linux"
echo "URL: $URL"
# Issue HEAD requests until the actual target is found.
# The result contains the target location, among some irrelevant stuff.
LOC=$(wget --no-verbose --method=HEAD --output-file - $URL)
echo "LOC: $LOC"
# Extract the URL from the result, stripping the irrelevant stuff.
URL=$(cut "--delimiter= " --fields=4 <<< "$LOC")
echo "URL: $URL"
# Optional: download the actual file.
wget "$URL"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.