linux下载网站所有,linux下面下载整个网站

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:])

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值