如何使用wget下载整个目录和子目录?

178

我正在尝试使用wget下载一个项目的文件,因为该项目的SVN服务器已经停止运行,我只能通过浏览器访问这些文件。所有文件的基本URL都相同,比如:

http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/*

我该如何使用wget(或其他类似的工具)下载该存储库中的所有文件,其中“tzivi”文件夹是根文件夹,并且在其下面有多个文件和子文件夹(最多有两到三级)?


3
如果服务器没有包含所有你需要的文件链接列表的网页,那么你就无法完成这个操作。 - Eddy_Em
1
你知道文件的名称吗? - Karoly Horvath
不好意思我不知道所有文件的名称。我尝试使用递归选项进行 wget,但它也没有起作用。那是因为服务器没有列出所有内部链接的 index.html 文件吗? - code4fun
你尝试过使用wget的镜像选项吗? - Tomasz Nguyen
10个回答

253
你可以在shell中使用以下命令:
wget -r --no-parent http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/

参数如下:
-r     //recursive Download

而且

--no-parent // Don´t download something from the parent directory

如果您不想下载全部内容,可以使用:

-l1 just download the directory (tzivi in your case)

-l2 download the directory and all level 1 subfolders ('tzivi/something' but not 'tivizi/somthing/foo')  

等等。如果您不插入-l选项,wget将自动使用-l 5

如果您插入-l 0,您将下载整个互联网,因为wget将跟随它找到的每个链接。


20
好的,为了让后面的读者更加易懂,简化一下:wget -r -l1 --no-parent http://www.stanford.edu/~boyd/cvxbook/cvxbook_additional_exercises/ 是我的答案。谢谢你的回答。 - isomorphismes
2
我尝试了上面的命令从http://websitename.com/wp-content/uploads/2009/05获取所有文件,但我只得到了一个没有任何内容的index.html文件。我搞不清楚我错过了什么。 - Vivek Todi
16
我知道这已经很老了。但是我也发现-e robots=off开关非常有用。;) - Matthias W.
2
为什么不直接修正答案,而要删除“我忘记了重要的东西”? - Nike
5
我们可以使用wget的-nH选项,防止下载目录默认创建主机名目录。 - Syed Faraz Umar
显示剩余5条评论

27
你可以在命令行中使用这个:
wget -r -nH --cut-dirs=7 --reject="index.html*" \
      http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/

参数如下:

-r recursively download

-nH (--no-host-directories) cuts out hostname 

--cut-dirs=X (cuts out X directories)

11

这个链接 给了我最好的答案:

$ wget --no-clobber --convert-links --random-wait -r -p --level 1 -E -e robots=off -U mozilla http://base.site/dir/

运行得非常顺畅。


这段代码在哪里应用? - Rajeev Ranjan Sharma

5

使用命令

wget -m www.ilanni.com/nexus/content/

5

2
您也可以使用这个命令:
wget --mirror -pc --convert-links -P ./your-local-dir/ http://www.your-website.com

这样可以确保您下载的网站镜像与原网站完全一致。


1

try this working code (30-08-2021):

!wget --no-clobber --convert-links --random-wait -r -p --level 1 -E -e robots=off --adjust-extension -U mozilla "yourweb directory with in quotations"

0

我无法让它工作。 无论我尝试什么,我都只得到一些http文件。

仅仅是下载一个目录的命令就已经这么复杂了吗? 肯定有更好的方法。 wget似乎不适合这个任务,除非它完全失败了。


-1

这个有效:

wget -m -np -c --no-check-certificate -R "index.html*" "https://the-eye.eu/public/AudioBooks/Edgar%20Allan%20Poe%20-%2"

-1
这会有所帮助。
wget -m -np -c --level 0 --no-check-certificate -R"index.html*"http://www.your-websitepage.com/dir

提供您建议答案的简短描述将更有帮助。请阅读https://stackoverflow.com/help/how-to-answer。 - 32cupo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接