我有一个web目录,我存储一些配置文件。我想使用wget将这些文件拉下来并保持它们当前的结构。例如,远程目录看起来像:
http://mysite.com/configs/.vim/
.vim包含多个文件和目录。我想用wget在客户端复制它。似乎无法找到正确的wget标志组合来完成这项工作。什么好主意吗?
我有一个web目录,我存储一些配置文件。我想使用wget将这些文件拉下来并保持它们当前的结构。例如,远程目录看起来像:
http://mysite.com/configs/.vim/
.vim包含多个文件和目录。我想用wget在客户端复制它。似乎无法找到正确的wget标志组合来完成这项工作。什么好主意吗?
当前回答
首先,感谢所有发帖的人。这是我递归下载一个网站的“终极”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)来运行JS可能是必要的。请注意,——convert-links选项仅在完成完整爬行之后才生效。
此外,如果你正在尝试wget一个网站,可能很快就会宕机,你应该与ArchiveTeam联系,让他们把你的网站添加到他们的ArchiveBot队列中。
其他回答
您应该使用-m (mirror)标志,因为这样可以避免混淆时间戳并无限地递归。
wget -m http://example.com/configs/.vim/
如果你加上其他人在这篇文章中提到的要点,它将是:
wget -m -e robots=off --no-parent http://example.com/configs/.vim/
您必须将-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
这个版本递归下载,不创建父目录。
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/”
递归wget忽略机器人(用于网站)
wget -e robots=off -r -np --page-requisites --convert-links 'http://example.com/folder/'
-e robots=off使它忽略该域的robots.txt
-r使它递归
-np = no parent,所以它不会跟随链接到父文件夹