备份豆瓣广播和日记教程

因为一些众所周知的原因,写在互联网的东西,很容易全部消失,备份豆瓣广播和日记到本地刻不容缓,于是搜索教程

备份广播和日记不是一个脚本,用到的技术工具Python,vscode

  1. 首先是备份豆瓣广播

    豆瓣广播py文件下载

    我需要的是md文件并符合hexo博客格式,所以我进行了修改

    原文件部分

    1
    2
    3
    4
    #your filepath to save data
    with open('your123.txt','a+',encoding='utf-8') as f:
    for item in results:
    f.write(item[1]+"\n"+item[0]+"\n\n")

    修改部分

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #your filepath to save data

    for item in results:
    #转换成时间数组
    dt = item[1]
    new_string=dt.strip('"')
    titleName = '微一说 '+new_string
    timeArray = time.strptime(new_string, "%Y-%m-%d %H:%M:%S")
    #转换成新的时间格式(20160505-20:28:54)
    dt_new = time.strftime("%Y%m%d%H%M%S",timeArray)
    dt_Name = '微一'+dt_new
    f = open(dt_Name+'.md','w+',encoding='utf-8')
    f.write('---'+"\n"+'title: '+titleName +"\n"+'author: andy'+"\n"+'date: '+new_string+"\n"+'tags: 微一说'+"\n"+'categories: 随笔'+"\n\n"+'---'+"\n"+item[0]+"\n"+'<!--more-->')
    f.close()

    原文件出现乱码,所以我添加了encoding=’utf-8’

  2. 备份豆瓣日记

    豆瓣广播py文件下载

    我需要的是md文件并符合hexo博客格式,所以我进行了修改

    原文件部分(会出错,定要修改)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #your filepath to save data
    def save_md(self, filetype):
    dirname = self.id + '豆瓣日记备份_' + filetype + '文件'
    os.makedirs(dirname,exist_ok=True)
    path = os.getcwd()
    for i in range(self.note_res_num):
    try:
    full_mdnote = '\n# ' + self.backupnote_title_list[i] + '\n##### ' \
    + self.backupnote_date_list[i] + '\n' + self.backupnote_text_list[i]
    filename = self.backupnote_date_list[i][0:10] + '_' + validate_title(self.backupnote_title_list[i])\
    + '.' + filetype
    with open(os.path.join(path,dirname,filename.replace('/','_')),'w',encoding='utf-8_sig') as f:
    f.write(full_mdnote)
    except Exception as e:
    print(f'保存第{i+1}篇日记失败。\n',e)
    print(f"成功备份共{self.note_res_num}篇日志为{filetype}文件!")

    修改后部分

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    def save_md(self, filetype):
    dirname = self.id + '豆瓣日记备份_' + filetype + '文件'
    os.makedirs(dirname,exist_ok=True)
    path = os.getcwd()
    for i in range(self.note_res_num):
    try:
    full_mdnote = '---' + "\n" + 'title: ' + self.backupnote_title_list[i] + "\n" + 'author: andy' + "\n" + 'date: ' + self.backupnote_date_list[i] + "\n" + 'tags: 日记' + "\n" + 'categories: 日记DB' + "\n" + '---' + "\n" + self.backupnote_text_list[i]
    filename = validate_title(self.backupnote_title_list[i])\
    + '.' + filetype
    with open(os.path.join(filename.replace('/','_')),'w',encoding='utf-8_sig') as f:
    f.write(full_mdnote)
    except Exception as e:
    print(f'保存第{i+1}篇日记失败。\n',e)
    print(f"成功备份共{self.note_res_num}篇日志为{filetype}文件!")
  3. 遇到的问题

    1. Python 3写入TXT出现’gbk’ codec can’t encode错误的解决办法

    2. python批量创建文件与批量创建文件夹

    3. Python中时间与时间戳之间的转换

    4. 如何用python实现从字符串中删除引号呢?

本文采用CC-BY-SA-3.0协议,转载请注明出处
Author: andy