我试图从产品的Facebook页面中提取一些数据并将其全部转储到文本文件中,但是我发现该文件不断用数据覆盖自身。我不确定这是一个分页问题还是必须制作多个文件。
这是我的代码:
#Modules import requests import facebook import json def some_action(post): print posts['data'] print post['created_time'] #Token access_token = 'INSERT ACCESS TOKEN' user = 'walkers' #Posts graph = facebook.GraphAPI(access_token) profile = graph.get_object(user) posts = graph.get_connections(profile['id'], 'posts') #Write while True: posts = requests.get(posts['paging']['next']).json() #print posts with open('test121.txt', 'w') as outfile: json.dump(posts, outfile)
知道为什么会这样吗?
这用于在文件w模式下使用文件运算符覆盖可以使用aappend方法的内容
w
a
可以这样做
修改:
with open('test121.txt', 'w') as outfile: while True: posts = requests.get(posts['paging']['next']).json() json.dump(posts, outfile)
w 覆盖现有文件
即
File1.txt:
123
码:
with open("File1.txt","w") as oup1: oup1.write("2")
python运行后的File1.txt:
2
那就是它的价值被覆盖
a 追加到现有文件
with open("File1.txt","a") as oup1: oup1.write("2")
1232
书面内容附在最后