我有一个 Web 目录,用于存储一些配置文件。我想使用 wget 将这些文件拉下来并保持它们当前的结构。例如,远程目录如下所示:
http://mysite.com/configs/.vim/
.vim 包含多个文件和目录。我想使用 wget 在客户端上复制它。似乎无法找到正确的 wget 标志组合来完成这项工作。有任何想法吗?
您必须将-np
/--no-parent
选项传递给wget
(当然,除了-r
/ --recursive
),否则它将按照我网站上目录索引中的链接指向父目录。所以命令看起来像这样:
wget --recursive --no-parent http://example.com/configs/.vim/
为避免下载自动生成的index.html
文件,请使用-R
/--reject
选项:
wget -r -np -R "index.html*" http://example.com/configs/.vim/
递归下载目录,拒绝 index.html* 文件并下载没有主机名、父目录和整个目录结构:
wget -r -nH --cut-dirs=2 --no-parent --reject="index.html*" http://mysite.com/dir1/dir2/data
对于其他有类似问题的人。Wget 跟随robots.txt
它可能不允许您抓取该站点。不用担心,您可以将其关闭:
wget -e robots=off http://www.example.com/
http://www.gnu.org/software/wget/manual/html_node/Robot-Exclusion.html
您应该使用 -m (镜像)标志,因为它注意不要弄乱时间戳并无限期地递归。
wget -m http://example.com/configs/.vim/
如果您在此线程中添加其他人提到的要点,它将是:
wget -m -e robots=off --no-parent http://example.com/configs/.vim/
这是完整的 wget 命令,可用于从服务器目录下载文件(忽略robots.txt
):
wget -e robots=off --cut-dirs=3 --user-agent=Mozilla/5.0 --reject="index.html*" --no-parent --recursive --relative --level=1 --no-directories http://www.example.com/archive/example/5.3.0/
如果--no-parent
没有帮助,您可以使用--include
选项。
目录结构:
http://<host>/downloads/good
http://<host>/downloads/bad
你想下载downloads/good
但不是downloads/bad
目录:
wget --include downloads/good --mirror --execute robots=off --no-host-directories --cut-dirs=1 --reject="index.html*" --continue http://<host>/downloads/good
wget -r http://mysite.com/configs/.vim/
为我工作。
也许您有一个干扰它的 .wgetrc ?
要使用用户名和密码递归地获取目录,请使用以下命令:
wget -r --user=(put username here) --password='(put password here)' --no-parent http://example.com/
此版本递归下载,不创建父目录。
wgetod() {
NSLASH="$(echo "$1" | perl -pe 's|.*://[^/]+(.*?)/?$|\1|' | grep -o / | wc -l)"
NCUT=$((NSLASH > 0 ? NSLASH-1 : 0))
wget -r -nH --user-agent=Mozilla/5.0 --cut-dirs=$NCUT --no-parent --reject="index.html*" "$1"
}
用法:
~/.bashrc
或粘贴到终端wgetod "http://example.com/x/"
您只需要两个标志,一个"-r"
用于递归和"--no-parent"
(或-np
),以便不进入'.'
and ".."
。像这样:
wget -r --no-parent http://example.com/configs/.vim/
就是这样。它将下载到以下本地树中:./example.com/configs/.vim
. 但是,如果您不想要前两个目录,请使用--cut-dirs=2
前面回复中建议的附加标志:
wget -r --no-parent --cut-dirs=2 http://example.com/configs/.vim/
它只会将您的文件树下载到./.vim/
事实上,我正是从wget 手册中得到了这个答案的第一行,他们在第 4.3 节的末尾有一个非常干净的例子。
在处理递归下载时,以下选项似乎是完美的组合:
wget -nd -np -P /dest/dir --recursive http://url/dir1/dir2
为方便起见,手册页中的相关片段:
-nd
--no-directories
Do not create a hierarchy of directories when retrieving recursively. With this option turned on, all files will get saved to the current directory, without clobbering (if a name shows up more than once, the
filenames will get extensions .n).
-np
--no-parent
Do not ever ascend to the parent directory when retrieving recursively. This is a useful option, since it guarantees that only the files below a certain hierarchy will be downloaded.
首先,感谢所有发布答案的人。这是我递归下载网站的“终极” wget 脚本:
wget --recursive ${comment# self-explanatory} \
--no-parent ${comment# will not crawl links in folders above the base of the URL} \
--convert-links ${comment# convert links with the domain name to relative and uncrawled to absolute} \
--random-wait --wait 3 --no-http-keep-alive ${comment# do not get banned} \
--no-host-directories ${comment# do not create folders with the domain name} \
--execute robots=off --user-agent=Mozilla/5.0 ${comment# I AM A HUMAN!!!} \
--level=inf --accept '*' ${comment# do not limit to 5 levels or common file formats} \
--reject="index.html*" ${comment# use this option if you need an exact mirror} \
--cut-dirs=0 ${comment# replace 0 with the number of folders in the path, 0 for the whole domain} \
$URL
之后,可能需要从 URL中剥离查询参数main.css?crc=12324567
并运行本地服务器(例如,通过python3 -m http.server
在您刚刚 wget'ed 的目录中)来运行 JS。请注意,该--convert-links
选项仅在完全爬网完成后才会生效。
此外,如果您想建立一个可能很快会关闭的网站,您应该联系 ArchiveTeam并要求他们将您的网站添加到他们的 ArchiveBot 队列中。
Wget 1.18 可能会更好,例如,我被 1.12 版本的错误咬住了...
wget --recursive (...)
...仅检索 index.html 而不是所有文件。
解决方法是注意一些 301 重定向并尝试新位置 - 给定新 URL,wget 获取目录中的所有文件。
递归 wget 忽略机器人(用于网站)
wget -e robots=off -r -np --page-requisites --convert-links 'http://example.com/folder/'
-e robots=off 导致它忽略该域的 robots.txt
-r 使其递归
-np = 没有父母,所以它不跟随到父文件夹的链接
听起来您正在尝试获取文件的镜像。虽然wget
有一些有趣的 FTP 和 SFTP 用途,但一个简单的镜像应该可以工作。只需几个注意事项即可确保您能够正确下载文件。
robots.txt
确保如果您的 、 或 目录中有文件,/robots.txt
它不会阻止爬网。如果是这样,您需要使用命令中的以下选项通过添加来指示忽略它:public_html
www
configs
wget
wget
wget -e robots=off 'http://your-site.com/configs/.vim/'
此外,wget
必须指示将链接转换为下载的文件。如果您已正确完成上述所有操作,那么您应该没问题。我发现获取所有文件的最简单方法是使用mirror
命令,前提是在非公共目录后面没有隐藏任何内容。
wget -mpEk 'http://your-site.com/configs/.vim/'
# If robots.txt is present:
wget -mpEk robots=off 'http://your-site.com/configs/.vim/'
# Good practice to only deal with the highest level directory you specify (instead of downloading all of `mysite.com` you're just mirroring from `.vim`
wget -mpEk robots=off --no-parent 'http://your-site.com/configs/.vim/'
首选使用-m
代替,-r
因为它没有最大递归深度并且它会下载所有资产。Mirror 非常擅长确定站点的完整深度,但是如果您有许多外部链接,您最终可能会下载的不仅仅是您的站点,这就是我们使用-p -E -k
. 制作页面的所有先决条件文件和保留的目录结构应该是输出。-k
将链接转换为本地文件。由于您应该设置一个链接,因此您应该在 config 文件夹中添加一个文件/.vim
.
镜像模式也适用于设置为的目录结构ftp://
。
根据您正在做镜像的站点的一侧,您正在向服务器发送许多调用。为了防止您被列入黑名单或被切断,请使用该wait
选项来限制您的下载。
wget -mpEk --no-parent robots=off --random-wait 'http://your-site.com/configs/.vim/'
但是,如果您只是下载../config/.vim/
文件,则不必担心它会忽略父目录并下载单个文件。
您应该只需添加 -r 就可以做到这一点
wget -r http://stackoverflow.com/