0

我正在从 Facebook 检索数据,检索到的数据为我提供了有关我的 Facebook 朋友喜欢的信息。

我写了这个:

import requests # pip install requests
import json
ACCESS_TOKEN='' #my Facebook access token here

base_url = 'https://graph.facebook.com/me'

# Get 10 likes for 10 friends
fields = 'id,name,friends.limit(10).fields(likes.limit(10))'

url = '%s?fields=%s&access_token=%s' %(base_url, fields, ACCESS_TOKEN,)

# This API is HTTP-based and could be requested in the browser,
# with a command line utlity like curl, or using just about
# any programming language by making a request to the URL.
# Click the hyperlink that appears in your notebook output
# when you execute this code cell to see for yourself...

print url

# Interpret the response as JSON and convert back
# to Python data structures

content = requests.get(url).json()

# Pretty-print the JSON and display it

print json.dumps(content, indent=1)

###############################More Options########################################
# Get all likes for 10 friends
fields = 'id,name,friends.limit(10).fields(likes)'
# Get all likes for 10 more friends
fields = 'id,name,friends.offset(10).limit(10).fields(likes)'
# Get 10 likes for all friends
fields = 'id,name,friends.fields(likes.limit(10))'

为了在 python 交互式 shell 上运行此代码,我使用execfile('Filename.py')了命令。现在我要做的是将检索到的数据存储在一个名为“likes.txt”的文本文件中。有谁知道如何做到这一点?

4

0 回答 0