月度归档: 2017 年 9 月

  • wget引发的惨案

    一场wget引发的惨案

    事情是这样的,从一个站点下载不少资料,直接用wget -m弄回来,然后就发现坑爹了,中文乱码了,,,而且乱的匪夷所思,有的汉字编码了,有的没有编码,还有空格的问题,那是要疯啊。。。

    这里先引用一下正解,然后再说我的解决方案

    reference: http://m13253.blogspot.tw/2013/04/solve-corrupted-chinese-filename-for-wget.html

    其实不必要像那样修改源代码,wget 的 man 页面里就有解决方法。
    正解是参数 --restrict-file-names=nocontrol
    Update: +筱百合 提供了他的 ~/.wgetrc 文件,方便大家借鉴:

    # 不要乱转义中文
    --restrict-file-names=nocontrol
    # 使用重定向后的文件名
    --trust-server-names=on
    --content-disposition=on

    ————————
    思路是
    1、提取当前目录下乱的名字(排除掉index.html本身)
    2、从index.html中提取正常中文名
    3、正常名字删掉前五行
    4、拼接乱的名字和正常名字
    5、生成改名脚本
    6、给权限执行mv index.html文件


    ls -cr| grep -v "index.html" >/root/testfd/messname.txt && python /root/testfd/getrealname.py >/root/testfd/realname.txt && sed '1,5d' /root/testfd/realname.txt > /root/testfd/real.txt && paste -d@ /root/testfd/messname.txt /root/testfd/real.txt > /root/testfd/namelist.txt && awk -F "@" '{print "mv \"" $1"\"","\""$2"\""}' /root/testfd/namelist.txt >changename.sh &&chmod +x changename.sh && mv index.html index.html.bak

    踩得一些坑
    生成的中间名文件不能扔到当前目录,不然会影响后面
    目录文件混在一起的时候会有bug,,,没解决

    getrealname.py


    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')

    from bs4 import BeautifulSoup

    soup = BeautifulSoup(open('index.html') , "html.parser")

    for string in soup.strings:
    print(string)