如何从风扇页面过滤出最活跃的用户?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (83)

我正在创建一个新的网站。我想用另一个我的主题相关的Web服务来推广它。我想送一些礼物给人们,这使我的第一个网站和扇页。如何过滤掉20个喜欢/分享/评论我的帖子的用户?

任何合适的编程语言都会很好。

提问于
用户回答回答于

在代码方面,可以使用任何东西,甚至可以使用简单的Web服务器(如MamP或Wamp等)或CLI在您的机器上本地运行。响应是所有的JSON,现代语言能够处理这个问题。下面是我介绍的Python第一个方法的快速示例:

import json
import urllib2
from collections import Counter

def search():
  req = urllib2.urlopen('https://graph.facebook.com/search?q=watermelon&type=post')
  res = json.loads(req.read())
  users = []

  for status in res['data']:
    users.append(status['from']['name'])

  count = Counter(users)

  print count.most_common()

if __name__ == '__main__':
  search()

当运行该代码时,它将返回该搜索中模式用户的有序列表(如那些使用特定搜索标记发布最多评论的用户)。如果希望使用第二种方法,可以很容易地对其进行调整。

用户回答回答于

$ python FacebookFanAnalyzer.py cern likes

$ python FacebookFanAnalyzer.py cern comments

$ python FacebookFanAnalyzer.py cern likes comments

注:不支持股票和内部评论。

文件:Facebook扇Analyzer.py

# -*- coding: utf-8 -*-
import json
import urllib2
import sys
from collections import Counter
reload(sys)
sys.setdefaultencoding('utf8')
###############################################################
###############################################################
#### PLEASE PASTE HERE YOUR TOKEN, YOU CAN GENERETE IT ON:
####    https://developers.facebook.com/tools/explorer
#### GENERETE AND PASTE NEW ONE, WHEN THIS WILL STOP WORKING

token = 'AjZCBe5yhAq2zFtyNS4tdPyhAq2zFtyNS4tdPw9sMkSUgBzF4tdPw9sMkSUgBzFZCDcd6asBpPndjhAq2zFtyNS4tsBphqfZBJNzx'

attrib_limit = 100
post_limit = 100
###############################################################
###############################################################


class FacebookFanAnalyzer(object):

    def __init__(self, fanpage_name, post_limit, attribs, attrib_limit):
        self.fanpage_name = fanpage_name
        self.post_limit = post_limit
        self.attribs = attribs
        self.attrib_limit = attrib_limit
        self.data={}

    def make_request(self, attrib):
        global token
        url = 'https://graph.facebook.com/' + self.fanpage_name + '/posts?limit=' + str(self.post_limit) + '&fields=' + attrib + '.limit('+str(self.attrib_limit)+')&access_token=' + token
        print "Requesting '" + attrib + "' data: " + url
        req = urllib2.urlopen(url)
        res = json.loads(req.read())

        if res.get('error'):
            print res['error']
            exit()

        return res

    def grep_data(self, attrib):
        res=self.make_request(attrib)
        lst=[]
        for status in res['data']:
            if status.get(attrib):
                for person in status[attrib]['data']:
                    if attrib == 'likes':
                        lst.append(person['name'])
                    elif attrib == 'comments':
                        lst.append(person['from']['name'])
        return lst


    def save_as_html(self, attribs):
        filename = self.fanpage_name + '.html'
        f = open(filename, 'w') 

        f.write(u'<html><head></head><body>')
        f.write(u'<table border="0"><tr>')
        for attrib in attribs:
            f.write(u'<td>'+attrib+'</td>')
        f.write(u'</tr>')

        for attrib in attribs:
            f.write(u'<td valign="top"><table border="1">')

            for d in self.data[attrib]:
                f.write(u'<tr><td>' + unicode(d[0]) + u'</td><td>' +unicode(d[1]) + u'</td></tr>')

            f.write(u'</table></td>')

        f.write(u'</tr></table>')
        f.write(u'</body>')
        f.close()
        print "Saved to " + filename

    def fetch_data(self, attribs):
        for attrib in attribs:
            self.data[attrib]=Counter(self.grep_data(attrib)).most_common()

def main():
    global post_limit
    global attrib_limit

    fanpage_name = sys.argv[1] 
    attribs = sys.argv[2:] 

    f = FacebookFanAnalyzer(fanpage_name, post_limit, attribs, attrib_limit)
    f.fetch_data(attribs)
    f.save_as_html(attribs)

if __name__ == '__main__':
    main()

产出:

Requesting 'comments' data: https://graph.facebook.com/cern/posts?limit=50&fields=comments.limit(50)&access_token=AjZCBe5yhAq2zFtyNS4tdPyhAq2zFtyNS4tdPw9sMkSUgBzF4tdPw9sMkSUgBzFZCDcd6asBpPndjhAq2zFtyNS4tsBphqfZBJNzx
Requesting 'likes' data: https://graph.facebook.com/cern/posts?limit=50&fields=likes.limit(50)&access_token=AjZCBe5yhAq2zFtyNS4tdPyhAq2zFtyNS4tdPw9sMkSUgBzF4tdPw9sMkSUgBzFZCDcd6asBpPndjhAq2zFtyNS4tsBphqfZBJNzx
Saved to cern.html

所属标签

可能回答问题的人

  • EatRice

    16 粉丝0 提问6 回答
  • 华讯云

    0 粉丝0 提问5 回答
  • 学生

    8 粉丝476 提问4 回答
  • 富有想象力的人

    5 粉丝0 提问3 回答

扫码关注云+社区

领取腾讯云代金券