fb 社團爬標題發文者內容
Graph API Explorer
import facebook
import pprint
token = ''
graph=facebook.GraphAPI(token)
groups = graph.get_connections(id='me', connection_name='groups')['data']
group_id = [group['id'] for group in groups if group['name'] == 'Jason_Test'][0]
feed = graph.request('/%s/feed' % group_id, args = {'limit': 1000})
for post in feed['data']:
if 'message' in post.keys():
if 'comments' in post.keys():
print post['message']
for i in range(len(post['comments']['data'])):
print post['comments']['data'][i]['from']['name']
print post['comments']['data'][i]['created_time']
print post['comments']['data'][i]['message']
else:
pass
else:
pass
Jason app 帳號
import facebook
import pprint
token = ''
graph=facebook.GraphAPI(token)
groups = graph.get_connections(id='me', connection_name='groups')['data']
group_id = [group['id'] for group in groups if group['name'] == 'Jason_Test'][0]
feed = graph.request('/%s/feed' % group_id, args = {'limit': 1000, 'fields' : "message,comments"})
for post in feed['data']:
if 'message' in post.keys():
if 'comments' in post.keys():
print post['message']
for i in range(len(post['comments']['data'])):
print post['comments']['data'][i]['from']['name']
print post['comments']['data'][i]['created_time']
print post['comments']['data'][i]['message']
else:
pass
else:
pass
Jason App 延長 token
import facebook
import pprint
import requests
def extend_access_token(access_token):
app_id = ""
client_secret = ""
link = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=" + app_id +"&client_secret=" + client_secret + "&fb_exchange_token=" + access_token
s = requests.Session()
token = s.get(link).content
token = token.split("&")[0]
token = token.strip("access_token=")
print token
def main():
token = ''
extend_access_token(token)
graph=facebook.GraphAPI(token)
groups = graph.get_connections(id='me', connection_name='groups')['data']
group_id = [group['id'] for group in groups if group['name'] == 'Jason_Test'][0]
feed = graph.request('/%s/feed' % group_id, args = {'limit': 1000, 'fields' : "message,comments"})
for post in feed['data']:
if 'message' in post.keys():
if 'comments' in post.keys():
print post['message']
for i in range(len(post['comments']['data'])):
print post['comments']['data'][i]['from']['name']
print post['comments']['data'][i]['created_time']
print post['comments']['data'][i]['message']
else:
pass
else:
pass
if __name__ == "__main__":
main()
update
import facebook
import pprint
import requests
def extend_access_token(access_token):
app_id = ""
client_secret = ""
link = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=" + app_id +"&client_secret=" + client_secret + "&fb_exchange_token=" + access_token
s = requests.Session()
token = s.get(link).content
token = token.split("&")[0]
token = token.strip("access_token=")
def main():
token = ''
extend_access_token(token)
graph=facebook.GraphAPI(token)
groups = graph.get_connections(id='me', connection_name='groups')['data']
group_id = [group['id'] for group in groups if group['name'] == 'Jason_Test'][0]
feed = graph.request('/%s/feed' % group_id, args = {'limit': 1000,'fields': "message,comments.fields(comments,likes,from,id,message,user_likes,created_time,like_count)"})
for post in feed['data']:
if 'message' in post.keys():
if 'comments' in post.keys():
print (u'標題:%s' % post['message'])
for p in post['comments']['data']:
print(u'日期:%s ID:%s 名字:%s 內容:%s' % \
(p['created_time'], \
p['from']['id'], \
p['from']['name'], \
p['message']))
if 'comments' in p.keys():
for pp in p['comments']['data']:
print(u'日期:%s ID:%s 名字:%s 內容:%s' % \
(pp['created_time'], \
pp['from']['id'], \
pp['from']['name'], \
pp['message']))
print '--------------------------------------------------------------'
else:
pass
else:
pass
raw_input()
if __name__ == "__main__":
main()
內容導向檔案
import facebook
import pprint
import requests
import sys
import codecs
def extend_access_token(access_token):
app_id = ""
client_secret = ""
link = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=" + app_id +"&client_secret=" + client_secret + "&fb_exchange_token=" + access_token
s = requests.Session()
token = s.get(link).content
token = token.split("&")[0]
token = token.strip("access_token=")
def main():
token = ''
extend_access_token(token)
graph=facebook.GraphAPI(token)
groups = graph.get_connections(id='me', connection_name='groups')['data']
group_id = [group['id'] for group in groups if group['name'] == 'Jason_Test'][0]
feed = graph.request(
'/%s/feed' % group_id,
args = {'limit': 1000,
'fields': "message,comments.fields(comments,likes,from,id,message,user_likes,created_time,like_count)"})
stdout_backup = sys.stdout
log_file = codecs.open("message.log", "w", "utf-8")
sys.stdout = log_file
for post in feed['data']:
if 'message' in post and 'comments' in post:
print (u'標題:%s' % post['message'])
for outer_data in post['comments']['data']:
outer_created_time = outer_data['created_time']
outer_id = outer_data['from']['id']
outer_name = outer_data['from']['name']
outer_message = outer_data['message']
print(u'日期:{outer_created_time} ID:{outer_id} 名字:{outer_name} 內容:{outer_message}'.format(**locals()))
if 'comments' in outer_data:
for inner_data in outer_data['comments']['data']:
inner_created_time = inner_data['created_time']
inner_id = inner_data['from']['id']
inner_name = inner_data['from']['name']
inner_message = inner_data['message']
print(u'日期:{inner_created_time} ID:{inner_id} 名字:{inner_name} 內容:{inner_message}'.format(**locals()))
print '--------------------------------------------------------------'
log_file.close()
sys.stdout = stdout_backup
print "Done."
raw_input()
if __name__ == "__main__":
main()
參數
1811390089074804/feed?fields=message,comments.fields(comments,likes,from,id,message,user_likes,created_time,like_count),from,link,id,to,likes.fields(id),created_time,actions,feed_targeting&limit=40
HTML
from html import HTML
inline_css={
'class1':'color:#00FF00;',
'class2':'color:#FF0000;',
'class3':'color:#FFFF00;',
}
b = HTML()
t = b.table(border='1')
r = t.tr()
r.td('column 1', style=inline_css['class1'])
r.td('column 2', style=inline_css['class2'])
r.td('column 3', style=inline_css['class3'])
print str(b)