Shell 使用wget递归获取包含任意文件的目录

Shell 使用wget递归获取包含任意文件的目录,shell,wget,Shell,Wget,我有一个web目录,其中存储了一些配置文件。我想使用wget来下拉这些文件并保持它们当前的结构。例如,远程目录如下所示: http://mysite.com/configs/.vim/ .vim保存多个文件和目录。我想使用wget在客户机上复制它。似乎找不到正确的wget标志组合来完成此操作。有什么想法吗 wget -r http://mysite.com/configs/.vim/ 对我有用 也许您有一个.wgetrc干扰了它?您只需添加一个-r就可以做到这一点 wget -r http:

我有一个web目录,其中存储了一些配置文件。我想使用wget来下拉这些文件并保持它们当前的结构。例如,远程目录如下所示:

http://mysite.com/configs/.vim/
.vim保存多个文件和目录。我想使用wget在客户机上复制它。似乎找不到正确的wget标志组合来完成此操作。有什么想法吗

wget -r http://mysite.com/configs/.vim/
对我有用


也许您有一个.wgetrc干扰了它?

您只需添加一个-r就可以做到这一点

wget -r http://stackoverflow.com/

您必须将
-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机器人=关闭http://www.example.com/

下面是一个完整的wget命令,我可以从服务器目录下载文件(忽略
robots.txt
):

如果
--no parent
没有帮助,您可以使用
--include
选项

目录结构:

http://<host>/downloads/good
http://<host>/downloads/bad
您应该使用-m(mirror)标志,因为这会注意不要弄乱时间戳并无限期地递归

wget -m http://example.com/configs/.vim/
如果您在该线程中添加其他人提到的点,则将是:

wget -m -e robots=off --no-parent http://example.com/configs/.vim/

要使用用户名和密码递归获取目录,请使用以下命令:

wget -r --user=(put username here) --password='(put password here)' --no-parent http://example.com/

WGET1.18可能工作得更好,例如,我被1.12版的bug咬了一口,其中

wget --recursive (...)
…仅检索index.html而不是所有文件


解决方法是注意到一些301重定向并尝试新的位置-给定新的URL,wget获得目录中的所有文件。

此版本以递归方式下载,不创建父目录

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
    ),以便不进入
    。像这样:

    wget-r——没有父级http://example.com/configs/.vim/

    就这样。它将下载到以下本地树中:
    /example.com/configs/.vim
    。 但是,如果您不想要前两个目录,请使用附加标志
    --cut dirs=2
    ,如前面答复中所建议:

    wget-r--无父项--剪切方向=2http://example.com/configs/.vim/

    它将只将您的文件树下载到
    /.vim/


    事实上,我从这个答案中得到的第一行正好来自,他们在第4.3节末尾有一个非常清晰的示例。

    在处理递归下载时,以下选项似乎是完美的组合:

    wget-nd-np-p/dest/dir——递归

    为方便起见,手册页中的相关片段:

       -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忽略机器人(用于网站)

    -e robots=off使其忽略该域的robots.txt

    -r使其递归


    -np=没有家长,因此它不会跟踪到家长文件夹的链接。首先,感谢所有发布答案的人。下面是我递归下载网站的“终极”wget脚本:

    wget——递归${comment#自解释}\
    --没有父${comment#不会对URL基础上方文件夹中的链接进行爬网}\
    --转换链接${comment#将域名链接转换为相对链接,将未绘制链接转换为绝对链接}\
    --随机等待--等待3--无http保持活动${注释#不被禁止}\
    --无主机目录${comment#不使用域名创建文件夹}\
    --执行robots=off--user-agent=Mozilla/5.0${comment#我是人类!!!}\
    --level=inf——接受“*”${comment#不限于5个级别或通用文件格式}\
    --reject=“index.html*”${comment#如果需要精确镜像,请使用此选项}\
    --cut dirs=0${comment#将0替换为路径中的文件夹数,0替换为整个域}\
    $URL
    
    之后,可能需要从URL(如
    main.css?crc=12324567
    )运行本地服务器(例如,通过刚刚创建的目录中的
    python3-mhttp.server
    )来运行JS。请注意,
    --convert links
    选项只有在完全爬网完成后才会生效


    此外,如果您正试图创建一个可能很快就会关闭的网站,您应该请求他们将您的网站添加到他们的ArchiveBot队列中。

    add-nH(剪切主机名)——cut dirs=X(剪切X目录)。手动计算X的目录有点烦人。为什么这些都不适用于?它只会下载机器人。txt@matteo因为robots.txt可能不允许在网站上爬行。您应该添加-e robots=off以强制爬网。如果您不想下载整个内容,可以使用:-l1只需下载目录(在您的情况下为example.com)-l2下载目录和所有级别1子文件夹('example.com/something'而不是'example.com/somthing/foo'),等等。如果插入no-l选项,wget将自动使用-l5。如果你插入一个-l0,你将下载整个互联网,因为wget将跟踪它找到的每个链接。为什么我总是得到index.html文件而不是目录<代码>wget-r--no parent-e robots=offhttp://demo.inspiretheme.com/templates/headlines/images/此命令将只获取
    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"
    }
    
       -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 -e robots=off -r -np --page-requisites --convert-links 'http://example.com/folder/'