使用API接口备份饭否消息为XML格式,Python3.0。
有人说API只能导出前3200条消息,其实是可以导出所有消息的,只不过用的参数不同而已。
导出为XML的缺点就是数据好大,我5700+消息导出来有10M。
另外还有一个问题就是饭否的bug了,导出的xml如果里面还有特殊unicode字符时会导致解析器抛异常,其实饭否输出的时候应该进行xmlencode一下的。
大概就是这样。下面是代码刷屏,过敏者请勿点击。
01.#!/usr/bin/env python 02.# -*- coding: UTF-8 -*- 03.from xml.dom import minidom 04.from urllib import request 05. 06.#代理设置 07.#request.install_opener(request.build_opener(request.ProxyHandler( 08.# {"http" : "http://192.168.60.250:8080"} 09.#))); 10. 11.#用户名 12.user = "bearice"; 13. 14.def loadPage(id=''): 15. url = "http://api.fanfou.com/statuses/user_timeline.xml?id=%s&;count=60&max_id=%s"%(user,id); 16. print('LOAD:%s'%url); 17. f = request.urlopen(url); 18. dom = minidom.parse(f); 19. f.close(); 20. return dom; 21. 22.i=0; 23.children = loadPage().documentElement.getElementsByTagName('status'); 24.out = open('fanfou.backup.xml','w',encoding='utf8'); 25.out.write('<?xml version="1.0" encoding="utf8"?>\n<statuses>\n'); 26.while (not len(children)<60): 27. if i>0: 28. children=children[1:]; 29. for st in children: 30. i+=1; 31. last=st.getElementsByTagName('id')[0].firstChild.data; 32. out.write(st.toxml()); 33. out.write('\n'); 34. print("%d:%s"%(i,last)); 35. children = loadPage(last).documentElement.getElementsByTagName('status'); 36.out.write('</statuses>\n'); 37.out.close();
1 条评论:
请问fanfou到底怎么了?
发表评论