新浪微博数据挖掘食谱之十四: 用户篇 (分析用户的粉丝和朋友)

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2015-1-10
@author: beyondzhou
@name: analyze_friends_followers.py
'''

# Analyze user's friends and followers
def analyze_friends_followers():
    
    # import 
    #import json
    from login import weibo_login
    from users import get_friends_followers_ids, setwise_friends_followers_analysis
    
    # Access to sina api
    weibo_api = weibo_login()
    
    screen_name = 'beyondzhou8'
    friends_ids, followers_ids = get_friends_followers_ids(weibo_api,
                                      screen_name=screen_name,
                                      friends_limit=10,
                                      followers_limit=10)
    
    setwise_friends_followers_analysis(screen_name, friends_ids, followers_ids)

if __name__ == '__main__':
    analyze_friends_followers()

# Setwise friends followers analysis
def setwise_friends_followers_analysis(screen_name, friends_ids, followers_ids):
    friends_ids, followers_ids = set(friends_ids), set(followers_ids)
    print '{0} is following {1}'.format(screen_name, len(friends_ids))
    print '{0} is being followed by {1}'.format(screen_name, len(followers_ids))
    print '{0} of {1} are not following {2} back'.format(
                       len(friends_ids.difference(followers_ids)),
                       len(friends_ids), screen_name)
    print '{0} of {1} are not being followed back by {2}'.format(
                       len(followers_ids.difference(friends_ids)),
                       len(followers_ids), screen_name)
    print '{0} has {1} mutual friends'.format(
                       screen_name, len(friends_ids.intersection(followers_ids)))
    
# Crawl a friendship graph
def crawl_weibo_followers(weibo_api, screen_name, limit=1000000, depth=2):
    
    from data import save_to_mongo
    
    # Resolve the ID for screen_name and start working with IDs for consistency in storage
    seed_id = str(weibo_api.users.show.get(screen_name=screen_name)['id'])
    _, next_queue = get_friends_followers_ids(weibo_api, user_id=seed_id, friends_limit=0, followers_limit=limit)
    
    # Store a seed_id => _follower_ids mapping in MongoDB
    save_to_mongo({'followers' : [ _id for _id in next_queue ]}, 'followers_crawl', '{0}-follower_ids'.format(seed_id))

    d = 1
    while d < depth:
        d += 1
        (queue, next_queue) = (next_queue, [])
        for fid in queue:
            follower_ids = get_friends_followers_ids(weibo_api, user_id=fid, friends_limit=0, followers_limit=limit)

            # Store a fid => follower_ids mapping in MongoDB
            save_to_mongo({'followers' : [ _id for _id in next_queue ]}, 'followers_crawl', '{0}-follower_ids'.format(fid))
            next_queue += follower_ids
callback_url: https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A//apps.weibo.com/guaguastd&response_type=code&client_id=2925245021
return_redirect_uri: http://weibo.com/login.php?url=http%3A%2F%2Fapps.weibo.com%2Fguaguastd%3Fcode%3D2924445625943200f9c7b3870bac7da1
code: ['2924445625943200f9c7b3870bac7da1']
Fetched 3 total friends ids for beyondzhou8
Fetched 3 total friends ids for beyondzhou8
Fetched 0 total followers ids for beyondzhou8
beyondzhou8 is following 3
beyondzhou8 is being followed by 0
3 of 3 are not following beyondzhou8 back
0 of 0 are not being followed back by beyondzhou8
beyondzhou8 has 0 mutual friends


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值