这里有点 Python 菜鸟。我有来自 Matthew Russell 的书“挖掘 Twitter 的 21 个食谱”和“挖掘社交网络”的 Python 代码,我想将其用于从 Twitter API 收集各种数据的项目,请在此处查看他的 github 页面:https:// github.com/ptwobrussel
我不知道的一件事是如何从用户和他/她的追随者/朋友之间的关系生成网络矩阵/图。例如,这是他在 Twitter 上收集用户朋友的 Python 代码(也在这里:https ://github.com/ptwobrussell/Recipes-for-Mining-Twitter/blob/master/recipe__get_friends_followers.py ):
# -*- coding: utf-8 -*-
import sys
import twitter
from recipe__make_twitter_request import make_twitter_request
import functools
SCREEN_NAME = sys.argv[1]
MAX_IDS = int(sys.argv[2])
if __name__ == '__main__':
# Not authenticating lowers your rate limit to 150 requests per hr.
# Authenticate to get 350 requests per hour.
t = twitter.Twitter(domain='api.twitter.com', api_version='1')
# You could call make_twitter_request(t, t.friends.ids, *args, **kw) or
# use functools to "partially bind" a new callable with these parameters
get_friends_ids = functools.partial(make_twitter_request, t, t.friends.ids)
# Ditto if you want to do the same thing to get followers...
# getFollowerIds = functools.partial(make_twitter_request, t, t.followers.ids)
cursor = -1
ids = []
while cursor != 0:
# Use make_twitter_request via the partially bound callable...
response = get_friends_ids(screen_name=SCREEN_NAME, cursor=cursor)
ids += response['ids']
cursor = response['next_cursor']
print >> sys.stderr, 'Fetched %i total ids for %s' % (len(ids), SCREEN_NAME)
# Consider storing the ids to disk during each iteration to provide an
# an additional layer of protection from exceptional circumstances
if len(ids) >= MAX_IDS:
break
# Do something useful with the ids like store them to disk...
print ids
因此,我设法以给定用户作为命令行参数的主要用户成功运行了此代码。但是我如何真正将这些数据放入一个矩阵中,然后我可以分析,运行公式(如中心性)等等......?到目前为止,我认为我可能需要使用可能包括 NetworkX、Redis 和 Matplotlib 的包的组合,但是实际生成这个矩阵的步骤让我望而却步。