代码之家  ›  专栏  ›  技术社区  ›  jerodsanto

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

  •  494
  • jerodsanto  · 技术社区  · 17 年前

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

    http://mysite.com/configs/.vim/
    

    12 回复  |  直到 17 年前
        1
  •  1085
  •   waldyrious    8 年前

    -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/
    
        2
  •  136
  •   Sri    15 年前

    要递归下载一个目录,该目录拒绝index.html*文件,下载时不包含主机名、父目录和整个目录结构:

    wget -r -nH --cut-dirs=2 --no-parent --reject="index.html*" http://mysite.com/dir1/dir2/data
    
        3
  •  124
  •   ma11hew28    12 年前

    对于其他有类似问题的人。工作组如下 robots.txt

    wget -e robots=off http://www.example.com/
    

    http://www.gnu.org/software/wget/manual/html_node/Robot-Exclusion.html

        4
  •  42
  •   ma11hew28    12 年前

    您应该使用-m(mirror)标志,因为这会注意不要弄乱时间戳并无限期地递归。

    wget -m http://example.com/configs/.vim/
    

    如果您在该线程中添加其他人提到的点,则将是:

    wget -m -e robots=off --no-parent http://example.com/configs/.vim/
    
        5
  •  40
  •   esote    9 年前

    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/
    
        6
  •  8
  •   user2288008 user2288008    13 年前

    如果 --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
    
        7
  •  7
  •   Conor McDermottroe    17 年前
    wget -r http://mysite.com/configs/.vim/
    

    对我有用。

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

        8
  •  5
  •   RomSteady    10 年前

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

    wget -r --user=(put username here) --password='(put password here)' --no-parent http://example.com/
    
        9
  •  3
  •   rkok    8 年前

    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"
    }
    

    用法:

    1. 加上 ~/.bashrc
    2. wgetod "http://example.com/x/"
        10
  •  3
  •   Jordan Gee    8 年前

    你只需要两个旗子,一个是 "-r" 用于递归和 "--no-parent" (或 -np )为了不让你进监狱 '.' ".."

    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 manual ,他们在第4.3节末尾有一个非常清晰的例子。

        11
  •  3
  •   pr-pal    6 年前

    wget-nd-np-P/dest/dir——递归 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.
    
        12
  •  2
  •   zb226 Steven Lizarazo    8 年前

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

    wget --recursive (...)
    

    …仅检索index.html而不是所有文件。

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

        13
  •  2
  •   berezovskyi    5 年前

    首先,感谢所有发布答案的人。下面是我递归下载网站的“终极”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
    

    stripping the query params 从URL像 main.css?crc=12324567 以及运行本地服务器(例如,通过 python3 -m http.server --convert-links 选项仅在完全爬网完成后生效。

    此外,如果你正试图建立一个网站,可能很快就会关闭,你应该 get in touch with the ArchiveTeam 并要求他们将您的网站添加到他们的ArchiveBot队列中。

        14
  •  1
  •   Tumelo Mapheto    6 年前

    递归wget忽略机器人(用于网站)

    wget -e robots=off -r -np --page-requisites --convert-links 'http://example.com/folder/'
    

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

    -r使其递归

    -np=没有父文件夹,因此它不跟踪父文件夹的链接

        15
  •  0
  •   kasperjj    17 年前

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

    wget -r http://stackoverflow.com/
    
        16
  •  0
  •   SentientFlesh    4 年前

    听起来你好像想得到你文件的镜像。虽然 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非常擅长确定网站的完整深度,但是如果你有很多外部链接,你最终可能下载的不仅仅是你的网站,这就是我们使用Mirror的原因 -p -E -k . 生成页面的所有必备文件以及保留的目录结构都应作为输出。 -k 将链接转换为本地文件。 因为您应该设置一个链接,所以您应该获得一个带有文件的配置文件夹 /.vim .

    镜像模式也适用于设置为 ftp:// 而且

    一般经验法则:

    根据您正在镜像的站点的哪一侧,您将向服务器发送许多调用。为了防止您被列入黑名单或被切断,请使用 wait 选项来限制您的下载。

    wget -mpEk --no-parent robots=off --random-wait 'http://your-site.com/configs/.vim/'
    

    但是如果你只是下载 ../config/.vim/ 文件您不必担心它,因为您将忽略父目录并下载单个文件。