简体   繁体   中英

How to get text from Facebook comments plugin with Python?

I am trying to get Facebook comments from the news site's comments plugin. This is the code I have at the moment:

import facebook
token = #valid token
graph = facebook.GraphAPI(access_token=token, version='2.10')
url = "http://www.jutarnji.hr/6703059"
url_id = graph.get_object(url)['og_object']['id']
comments = graph.get_connections(url_id, 'comments')

And this is the (unwanted) output:

{'data': []}

You can try this code

import facebook    #sudo pip install facebook-sdk
import itertools
import json
import re
import requests

access_token = "XXX"
user = 'leehsienloong'

graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')

Jstr = json.dumps(posts)
JDict = json.loads(Jstr)

count = 0
for i in JDict['data']:
    allID = i['id']
    try:
        allComments = i['comments']

        for a in allComments['data']:  
            count += 1
            print a['message']


    except (UnicodeEncodeError):
        pass


print count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM