使用 wget 递归下载的问题

Kar*_*tik 5 download wget internet

我正在尝试wget从 wget 信息页面学习如何使用递归下载。

例如,让我们尝试下载xkcd 的所有图像。所有页面的列表都存在于xkcd 档案中。所有页面中都有一个 png 文件。png 文件位于不同的主机 imgs.xkcd.com 中。

我试过这个命令:

wget -r -HD imgs.xkcd.com -l 2 -A.png http://www.xkcd.com/archive/ --random-wait
Run Code Online (Sandbox Code Playgroud)

结果:

 xkcd $ tree
.

0 directories, 0 files

 xkcd $ wget -r -HD imgs.xkcd.com -l 2 -A.png http://www.xkcd.com/archive/ --random-wait
--2014-01-10 18:49:55--  http://www.xkcd.com/archive/
Resolving www.xkcd.com (www.xkcd.com)... 107.6.106.82
Connecting to www.xkcd.com (www.xkcd.com)|107.6.106.82|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 83226 (81K) [text/html]
Saving to: `www.xkcd.com/archive/index.html'

100%[=============================================================================================================>] 83,226      68.3K/s   in 1.2s    

2014-01-10 18:49:57 (68.3 KB/s) - `www.xkcd.com/archive/index.html' saved [83226/83226]

Loading robots.txt; please ignore errors.
--2014-01-10 18:49:57--  http://imgs.xkcd.com/robots.txt
Resolving imgs.xkcd.com (imgs.xkcd.com)... 107.6.106.82
Reusing existing connection to www.xkcd.com:80.
HTTP request sent, awaiting response... 404 Not Found
2014-01-10 18:49:58 ERROR 404: Not Found.

Removing www.xkcd.com/archive/index.html since it should be rejected.

--2014-01-10 18:49:58--  http://imgs.xkcd.com/static/terrible_small_logo.png
Reusing existing connection to www.xkcd.com:80.
HTTP request sent, awaiting response... 200 OK
Length: 11001 (11K) [image/png]
Saving to: `imgs.xkcd.com/static/terrible_small_logo.png'

100%[=============================================================================================================>] 11,001      --.-K/s   in 0.05s   

2014-01-10 18:49:58 (229 KB/s) - `imgs.xkcd.com/static/terrible_small_logo.png' saved [11001/11001]

FINISHED --2014-01-10 18:49:58--
Total wall clock time: 2.9s
Downloaded: 2 files, 92K in 1.2s (74.4 KB/s)

 xkcd $ tree
.
|-- imgs.xkcd.com
|   `-- static
|       `-- terrible_small_logo.png
`-- www.xkcd.com
    `-- archive

4 directories, 1 file

 xkcd $
Run Code Online (Sandbox Code Playgroud)

这显然不是我想要的。似乎 wgetwww.xkcd.com/archive/index.html 阅读和检查链接之前被拒绝了。即使.html添加到接受列表(如答案中所建议),它也不会下载图像。命令中的错误是什么?

Jen*_*y D 3

问题是您对要跟踪的链接的限制。您已将其设置为仅跟踪 imgs.xkcd.com 的链接。但 /archive/ 页面不直接包含任何链接 - 它只包含指向 www.xkcd.com 上其他页面的链接,然后这些页面包含指向 imgs.xkcd.com 的链接。

因此,您也需要允许该域。该命令的工作原理:

wget -r -HD imgs.xkcd.com,www.xkcd.com -l 2 -A.png http://www.xkcd.com/archive/ --random-wait
Run Code Online (Sandbox Code Playgroud)