首页 > 代码库 > wget下载整个网站的方法

wget下载整个网站的方法

转自: http://blog.itpub.net/29867/viewspace-716088/ 

(修改部分内容)

wget --restrict-file-name=ascii -m -c -nv -np -k -E -p http://www.w3school.com.cn/
wget --restrict-file-name=ascii -m -c -nv -np -k -E -p http://scrapy-chs.readthedocs.org

参数释义如下:

--restrict-file-name=ascii ,将文件名保存为ASCII格式。这样能避免utf-8文件名带来的麻烦(注:1.12版才支持ascii参数值)

-m 整站下载,mirror的缩写,是-N -r -l inf --no-remove-listing 这几个参数的快捷方式,具体详阅各自的说明

-c 续传

-nv 不显示详细的下载详情

-np don’t ascend to the parent directory.即下载的Web页面不越过后面指定的 http://www.xxx.com的范围。当然,如果你指定的是 http://www.xxx.com/aaa,则所有的web页面都要在 http://www.xxx.com/aaa下

-k 下载完成后,将页面文件中的链接转换为本地链接,便于离线浏览和制作chm等

-E 保存html/css文件时,使用合适的文件后缀。例如,在某些网站有些文件是服务器端动态生成的,虽然是css文件,但后缀并不是css,-E选项可以调整之

-p -np对页面文件做了限制,如果不加-p,则html所需的媒体文件也会受限于-np,-p则会下载html/css文件所需的所有媒体文件(图片、音频、视频等)

-R 拒绝下载的文件后缀列表,逗号分隔

 

至于下载到的文件的文件名变为了形如%A7这样百分号加16进制数字的形式,可以用个python程序来改变文件名:

————————————————————————————————————

import os, urllib, sys, getopt

class Renamer:

    input_encoding = ""

    output_encoding = ""

    path = ""

    is_url = False

    def __init__(self, input, output, path, is_url):

        self.input_encoding = input

        self.output_encoding = output

        self.path = path

        self.is_url = is_url

    def start(self):

        self.rename_dir(self.path)

    def rename(self, root, path):

        try:

            if self.is_url:

                new = urllib.unquote(path).decode(self.input_encoding).encode(self.output_encoding)

            else:

                new = path.decode(self.input_encoding).encode(self.output_encoding)

            os.rename(os.path.join(root, path), os.path.join(root, new))

        except:

            pass

    def rename_dir(self, path):

        for root, dirs, files in os.walk(path):

            for f in files:

                self.rename(root, f)

            if dirs == []:

                for f in files:

                    self.rename(root, f)

            else:

                for d in dirs:

                    self.rename_dir(os.path.join(root, d))

                    self.rename(root, d)

def usage():

    print ‘‘‘This program can change encode of files or directories.

    Usage:   rename.py [OPTION]...

    Options:

        -h, --help                  this document.

        -i, --input-encoding=ENC    set original encoding, default is UTF-8.

        -o, --output-encoding=ENC   set output encoding, default is GBK.

        -p, --path=PATH             choose the path which to process.

        -u, --is-url                whether as a URL

    ‘‘‘

def main(argv):

    input_encoding = "utf-8"

    output_encoding = "gbk"

    path = ""

    is_url = True

    try:

        opts, args = getopt.getopt(argv, "hi:o:p:u", ["help", "input-encoding=", "output-encoding=", "path=", "is-url"])

    except getopt.GetoptError:

        usage()

        sys.exit(2)

    for opt, arg in opts:

        if opt in ("-h", "--help"):

            usage()

            sys.exit()

        elif opt in ("-i", "--input-encoding"):

            input_encoding = arg

        elif opt in ("-o", "--output-encoding"):

            output_encoding = arg

        elif opt in ("-p", "--path"):

            path = arg

        elif opt in ("-u", "--is-url"):

            is_url = True

    rn = Renamer(input_encoding, output_encoding, path, is_url)

    rn.start()

if __name__ == ‘__main__‘:

    main(sys.argv[1:])

————————————————————————————————————

rename.py -i utf-8 -o gbk -p <指定的下载目录> -u

文件改名方法来自于http://blog.csdn.net/kowity/article/details/6899256

wget下载整个网站的方法