Python 爬行Twitter用户的Friendship图


CODE:

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

'''
Created on 2014-8-1
@author: guaguastd
@name: crawing_friendship_graph.py
'''

if __name__ == '__main__':
    
    # import json
    #import json
    
    # import search
    from search import search_for_tweet
    
    # import get_friends_followers_ids
    from user import crawl_followers
    
    # import login, see http://blog.csdn.net/guaguastd/article/details/31706155
    from login import twitter_login

    # get the twitter access api
    twitter_api = twitter_login()
    
    # import twitter_text
    import twitter_text
    
    while 1:
        query = raw_input('\nInput the query (eg. #MentionSomeoneImportantForYou, exit to quit): ')
        
        if query == 'exit':
            print 'Successfully exit!'
            break
        
        statuses = search_for_tweet(twitter_api, query) 
        ex = twitter_text.Extractor(statuses)     
        
        screen_names = ex.extract_mentioned_screen_names_with_indices()
        screen_names = [screen_name['screen_name']
                        for screen_name in screen_names]
                
        for screen_name in screen_names:
            #print json.dumps(screen_names, indent=1)  
            crawl_followers(twitter_api, screen_name, depth=1, limit=10)

RESULT:

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): Mining Twitter
Length of statuses 88
Fetched 5000 total followers ids for 15362680
[{u'_id': ObjectId('53dacba0ae6f2217909a0e04'), u'followers': [2371121161L, 403929334, 2696296824L, 2696169230L, 310282374, 315832952, 2450621492L, 1261558134, 16996451, 2440864703L]}, {u'_id': ObjectId('53dacdc8ae6f22172c317a79'), u'followers': [2371121161L, 403929334, 2696296824L, 2696169230L, 310282374, 315832952, 2450621492L, 1261558134, 16996451, 2440864703L]}] 
Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): 


评论关闭