zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python 查找Twitter中特定话题中最流行的10个转发Tweet

Python 10 查找 转发 特定 流行 twitter 话题
2023-09-11 14:15:00 时间

CODE:

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

'''
Created on 2014-7-4
@author: guaguastd
@name: find_popular_retweets.py
'''
    
if __name__ == '__main__':

    # 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 search
    from search import search_for_tweet
    
    # import tweet
    from tweet import popular_retweets
    
    # pip install prettytable
    from prettytable import PrettyTable
    
    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)
        retweets = popular_retweets(statuses)
        
        # Slice off the first 10 from the sorted results and display each item in the tuple
        pt = PrettyTable(field_names=['Count', 'Screen Name', 'Text'])
        [pt.add_row(row) for row in sorted(retweets, reverse=True)[:10]]
        pt.max_width['Text'] = 50
        pt.align = 'l'
        print pt

RESULT:

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): #MentionSomeoneImportantForYou
Length of statuses 30
+-------+----------------+----------------------------------------------------+
| Count | Screen Name    | Text                                               |
+-------+----------------+----------------------------------------------------+
| 2     | xmlovex        | RT @xmlovex: #MentionSomeoneImportantForYou        |
|       |                | @purpledrauhl_23                                   |
| 1     | thuggie_salma  | RT @thuggie_salma: "@KillahPimpp:                  |
|       |                | #MentionSomeoneImportantForYou @thuggie_salma"     |
|       |                | baeee