如何使用wget/curl下载给定网页上所有.zip文件的链接?

86

一个页面包含一组.zip文件的链接,我希望将它们全部下载。我知道可以使用wget和curl来完成,具体该如何操作呢?

3个回答

137
命令如下:

该命令是:

wget -r -np -l 1 -A zip http://example.com/download/

选项的含义:

-r,  --recursive          specify recursive download.
-np, --no-parent          don't ascend to the parent directory.
-l,  --level=NUMBER       maximum recursion depth (inf or 0 for infinite).
-A,  --accept=LIST        comma-separated list of accepted extensions.

16
如果你不想要创建额外的文件夹(也就是说,所有文件都在根目录下),那么-nd(无目录)标志很方便。 - Steve Davis
1
我该如何调整这个解决方案,使其从给定页面更深入地进行?我尝试了-l 20,但wget立即停止。 - Wrench
2
如果文件不在起始URL所在的同一目录中,您可能需要去掉“-np”。如果它们位于不同的主机上,则需要使用“--span-host”。 - Dan
有没有一种方法可以保留网站的目录结构,但仅排除根文件夹,使当前文件夹成为网站的根文件夹,而不是一个带有网站URL名称的文件夹? - Aaron Franke

94
这个解决方案对我没用。 只有这个才有效:
wget -r -l1 -H -t1 -nd -N -np -A.mp3 -erobots=off [url of website]

选项的含义:

-r            recursive
-l1           maximum recursion depth (1=use only this directory)
-H            span hosts (visit other hosts in the recursion)
-t1           Number of retries
-nd           Don't make new directories, put downloaded files in this one
-N            turn on timestamping
-A.mp3        download only mp3s
-erobots=off  execute "robots.off" as if it were a part of .wgetrc

2
使用wget下载网站上的所有音乐文件 - James Jeffery
是的,谢谢!我不记得它来自哪里了,它只是躺在我的脚本里。 - K.-Michael Aye
1
+1 是针对 -H 开关的。这就是导致第一个答案(也是我在 SO 上查找之前尝试的)无法正常工作的原因。 - Alex
1
不对,你在2013年9月10日回答过这个问题。 - Quasímodo
1
哦,我一定是想错了,九月应该是7月!谁把这个月变成第九个月的?(当然,是罗马人...) - K.-Michael Aye
显示剩余4条评论

7

对于其他需要一些并行魔法的场景,我使用:

curl [url] | grep -i [filending] | sed -n 's/.*href="\([^"]*\).*/\1/p' |  parallel -N5 wget -

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