コード例 #1
0
requests = [level1[i:i + 100] for i in range(0, len(level1), 100)]
for request in requests:
    users = tf.get_users(request, auth_api)
    for user in users:
        if user[3] <= 100 and user[3] >= 25:
            followers.append(user[0])  # returns 13,416 followers

#randomly select 5000 followers to explore network
rand_followers = random.sample(followers, 5000)

#%% for each of Hadley's followers, find all accounts they follow
# and tally up how many of Hadley's followers also follow each of these accounts
level2 = defaultdict(int)
for follower in rand_followers:
    try:
        for friend in tf.get_friend_ids(follower, auth_api):
            level2[friend] += 1
    except:
        print("Error. Skipping", follower)

with open("hadley_level2.pkl", "wb") as file:
    pickle.dump(level2, file)

#%%
#for all level2 followers with more than 5 common followers
accounts = [account for account in level2 if level2[account] >= 5]

#separate into batches of 100 for twitter API
account_batches = [accounts[i:i + 100] for i in range(0, len(accounts), 100)]

#collect account info for level2 accounts, calculate jaccard similarity