新浪微博数据挖掘食谱之十二: 用户篇 (批量获取用户的粉丝数和朋友数)
2015-01-08 05:27
483 查看
#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-8 @author: beyondzhou @name: fetch_friends_followers.py ''' # Fetch user friends followers id def fetch_friends_followers(): # import #import json from login import weibo_login from users import get_friends_followers_ids # Access to sina api weibo_api = weibo_login() friends_ids, followers_ids = get_friends_followers_ids(weibo_api, screen_name="beyondzhou8", friends_limit=10, followers_limit=10) print friends_ids print followers_ids if __name__ == '__main__': fetch_friends_followers()
# Getting all friends or followers for a user def get_friends_followers_ids(weibo_api, screen_name=None, user_id=None, friends_limit=maxint, followers_limit=maxint): # import sys import sys # import functools, sys from functools import partial # import make_twitter_request from login import make_weibo_request # Must have either screen_name or user_id (logical xor) assert (screen_name != None) != (user_id != None), \ "Must have screen name or user_id, but not both" # See http://open.weibo.com/wiki/2/friendships/friends/ids and # http://open.weibo.com/wiki/2/friendships/followers/ids for details # on API parameters get_friends_ids = partial(make_weibo_request, weibo_api.friendships.friends.ids.get, count=5000) get_followers_ids = partial(make_weibo_request, weibo_api.friendships.followers.ids.get, count=5000) friends_ids, followers_ids = [], [] for weibo_api_func, limit, ids, label in [ [get_friends_ids, friends_limit, friends_ids, "friends"], [get_followers_ids, followers_limit, followers_ids, "followers"] ]: if limit == 0: continue cursor = -1 while cursor != 0: # Use make_weibo_request via the partially bound callable... if screen_name: response = weibo_api_func(screen_name=screen_name, cursor=cursor) else: response = weibo_api_func(user_id=user_id, cursor=cursor) if response is not None: ids += response['ids'] cursor = response['next_cursor'] print >> sys.stderr, 'Fetched {0} total {1} ids for {2}'.format(len(ids), label, (user_id or screen_name)) # You may want to store data during each iteration to provide an additional # layer of protectio from exceptional circumstances if len(ids) >= limit or response is None: break # Do something useful with the IDs, like store them to disk... return friends_ids[:friends_limit], followers_ids[:followers_limit]
Result:
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%3Dde47ae74f35c09dadf555621df5a6591 code: ['de47ae74f35c09dadf555621df5a6591'] Fetched 3 total friends ids for beyondzhou8 Fetched 3 total friends ids for beyondzhou8 Fetched 0 total followers ids for beyondzhou8 [1826958817, 1650042843, 1784501333] []
相关文章推荐
- 新浪微博数据挖掘食谱之十一: 用户篇 (批量获取用户信息)
- 新浪微博数据挖掘食谱之十四: 用户篇 (分析用户的粉丝和朋友)
- 新浪微博数据挖掘食谱之十三: 微博篇 (批量获取用户的微博)
- 新浪微博数据挖掘食谱之十五: 爬虫篇 (抓取用户的朋友)
- 新浪微博数据挖掘食谱之九: 用户篇 (获取转发微博的用户名)
- 新浪微博如何挖掘大数据资源为用户带来新价值
- 新浪微博如何挖掘大数据资源为用户带来新价值
- 新浪微博如何挖掘大数据资源为用户带来新价值
- 新浪微博数据挖掘食谱之十六: 微博篇 (词汇差异性,词汇均值)
- 新浪微博如何挖掘大数据资源为用户带来新价值
- 新浪微博数据挖掘食谱之七: 查询篇 (查询最流行的微博)
- 新浪微博数据挖掘食谱之二: 话题篇 (selenium)
- 如何利用新浪微博开放平台API获取新浪微博用户数据
- 新浪微博如何挖掘大数据资源为用户带来新价值
- 数据挖掘技术与用户知识获取
- 新浪微博数据挖掘食谱之四: 保存篇 (json text格式)
- 新浪微博数据挖掘食谱之六: 元素篇 (提取微博元素)
- 33day 新浪微博(获取用户微博数据)
- 新浪微博数据挖掘食谱之十: 元素篇 (提取转发微博的元素)
- 新浪微博数据挖掘食谱之三: 搜索篇 (selenium)