Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7390272
  • 博文数量: 1755
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16227
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1755)

文章存档

2024年(1)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: LINUX

2011-11-22 15:09:33

最近发现一个很好的网站:,里面有大量的技术书籍。于是想使用 wget 把整个网站都下载下来。但是 wget 对中文的 url 支持得不够好,直接使用:

  wget -m

下载的话,中文文件名就会乱码,比如“2010架构师大会PPT”就变成了“2010鏋舵瀯甯堝ぇ浼歅PT”。

  wget --restrict-file-name=ascii -m

下载的话,中文文件名会编码成URL形式,比如比如“2010架构师大会PPT”就变成了“2010%E6%9E%B6%E6%9E%84%E5%B8%88%E5%A4%A7%E4%BC%9APPT”。主要是因为在网页上,中文 UR L会以 UTF-8 来编码,而 Windows 存储文件名是用GBK编码。也就是说“2010鏋舵瀯甯堝ぇ浼歅PT”实际上是以 GBK 编码来显示的 UTF-8 编码的文件名。这样我们只要用 Python 写个编码转换器就可以了。代码如下:


  1. import os, urllib, sys, getopt
  2. class Renamer:
  3. input_encoding = ""
  4. output_encoding = ""
  5. path = ""
  6. is_url = False
  7. def __init__(self, input, output, path, is_url):
  8. self.input_encoding = input
  9. self.output_encoding = output
  10. self.path = path
  11. self.is_url = is_url
  12. def start(self):
  13. self.rename_dir(self.path)
  14. def rename(self, root, path):
  15. try:
  16. if self.is_url:
  17. new = urllib.unquote(path).decode(self.input_encoding).encode(self.output_encoding)
  18. else:
  19. new = path.decode(self.input_encoding).encode(self.output_encoding)
  20. os.rename(os.path.join(root, path), os.path.join(root, new))
  21. except:
  22. pass
  23. def rename_dir(self, path):
  24. for root, dirs, files in os.walk(path):
  25. for f in files:
  26. self.rename(root, f)
  27. if dirs == []:
  28. for f in files:
  29. self.rename(root, f)
  30. else:
  31. for d in dirs:
  32. self.rename_dir(os.path.join(root, d))
  33. self.rename(root, d)
  34. def usage():
  35. print '''''This program can change encode of files or directories.
  36. Usage: rename.exe [OPTION]...
  37. Options:
  38. -h, --help this document.
  39. -i, --input-encoding=ENC set original encoding, default is UTF-8.
  40. -o, --output-encoding=ENC set output encoding, default is GBK.
  41. -p, --path=PATH choose the path which to process.
  42. -u, --is-url whether as a URL
  43. '''
  44. def main(argv):
  45. input_encoding = "utf-8"
  46. output_encoding = "gbk"
  47. path = ""
  48. is_url = True
  49. try:
  50. opts, args = getopt.getopt(argv, "hi:o:p:u", ["help", "input-encoding=", "output-encoding=", "path=", "is-url"])
  51. except getopt.GetoptError:
  52. usage()
  53. sys.exit(2)
  54. for opt, arg in opts:
  55. if opt in ("-h", "--help"):
  56. usage()
  57. sys.exit()
  58. elif opt in ("-i", "--input-encoding"):
  59. input_encoding = arg
  60. elif opt in ("-o", "--output-encoding"):
  61. output_encoding = arg
  62. elif opt in ("-p", "--path"):
  63. path = arg
  64. elif opt in ("-u", "--is-url"):
  65. is_url = True
  66. rn = Renamer(input_encoding, output_encoding, path, is_url)
  67. rn.start()
  68. if __name__ == '__main__':
  69. main(sys.argv[1:])
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.exe [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:])


如果 wget 是使用以下命令行来下载:

  wget --restrict-file-name=ascii -m

那么下载下来的文件是“2010%E6%9E%B6%E6%9E%84%E5%B8%88%E5%A4%A7%E4%BC%9APPT”形式,运行脚本时就使用以下命令:

  rename.py -i utf-8 -o gbk -p R:\ebook.elain.org -u

下载后的结果如下图。

转:http://blog.csdn.net/kowity/article/details/6899256

阅读(2277) | 评论(0) | 转发(0) |
0

上一篇:gitblit 安装

下一篇:mantisbt-记录

给主人留下些什么吧!~~