如何使用结束尾部斜杠在网站上进行 wget,并像没有尾随斜杠一样保存



我用Wget创建了一个供个人使用的爬虫。

wget -k -m -Dwww.website.com -r -q -R gif,png,jpg,jpeg,GIF,PNG,JPG,JPEG,js,rss,xml,feed,.tar.gz,.zip,rar,.rar,.php,.txt -t 1 http://www.website.com/ &

网站中的帖子示例URL是http://www.website.com/post-one/,每个帖子的URL末尾都有尾部斜杠。

保存后,Wget 将创建:

www.website.net/post-one
www.website.net/post-one/index.html
第一行是

文件夹,而第二行是我正在寻找的实际HTML文件。问题是,Wget 会为每个帖子创建一个文件夹,这使得处理数据变得更加困难。

我希望 Wget 创建www.website.net/post-one这是 HTML 文件post-one而不是为每个帖子创建文件夹。

我尝试了很多方法,但没有运气。使用没有内容-R .html结果文件夹。

我使用的 wget 支持以下目录选项:

-nd, --no-directories           don't create directories.
-x,  --force-directories        force creation of directories.
-nH, --no-host-directories      don't create host directories.
     --protocol-directories     use protocol name in directories.
-P,  --directory-prefix=PREFIX  save files to PREFIX/...
     --cut-dirs=NUMBER          ignore NUMBER remote directory component

也许 -nd 或 -P 可以帮助你。

否则,在使用现有 wget 下载所有文件后,shell 脚本可以轻松地将文件转换为单级目录。

#!/bin/bash
cd www.website.net
for d in $( find . -type -d -print ) ; do
   if [[ -f $d/index.html ]] ; then
     echo mv $d/index.html $.html && echo rmdir $d
    fi
done

当您确定循环正在生成适合您的输出时,请删除echo S。

我希望这有所帮助。

P.S. 由于您似乎是新用户,如果您得到一个对您有帮助的答案,请记住将其标记为已接受,和/或给它一个 +(或 -)作为有用的答案。

相关内容

最新更新