简体   繁体   English

如何使用 Python 从 Facebook 评论插件中获取文本?

[英]How to get text from Facebook comments plugin with Python?

I am trying to get Facebook comments from the news site's comments plugin.我试图从新闻网站的评论插件中获取 Facebook 评论。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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