Python 查找Twitter推文中最流行的推文元素

CODE:

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

'''
Created on 2014-7-22
@author: guaguastd
@name: find_popular_entities.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 twitter_search
    
    # import tweet
    from tweet import get_common_tweet_entities
    
    # 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
        
        search_results = twitter_search(twitter_api, query, max_results=100)
        common_entities = get_common_tweet_entities(search_results)
        
        # Slice off the first 100 from the sorted results and display each item in the tuple
        pt = PrettyTable(field_names=['Count', 'Entity'])
        [pt.add_row(row) for row in sorted(common_entities, reverse=True)[:100]]
        pt.max_width['Text'] = 50
        pt.align = 'l'
        print pt
        
        #print "Most common tweet entities"
        #print common_entities

RESULT:

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): Python
Length of statuses 100
+-------+----------------------------------------------------------------------------------------------------------------------------------------------+
| Count | Entity                                                                                                                                       |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------+
| 118   | Python                                                                                                                                       |
| 71    | Monty                                                                                                                                        |
| 57    | RT                                                                                                                                           |
| 49    | the                                                                                                                                          |
| 43    | python                                                                                                                                       |
| 30    | a                                                                                                                                            |
| 30    | I                                                                                                                                            |
| 29    | for                                                                                                                                          |
| 29    | -                                                                                                                                            |
| 27    | and                                                                                                                                          |
| 25    | to                                                                                                                                           |
| 24    | How                                                                                                                                          |
| 23    | says                                                                                                                                         |
| 23    | of                                                                                                                                           |
| 23    | goodbye                                                                                                                                      |
| 22    | last                                                                                                                                         |
| 21    | is                                                                                                                                           |
| 18    | from                                                                                                                                         |
| 17    | with                                                                                                                                         |
| 17    | show                                                                                                                                         |
| 17    | The                                                                                                                                          |
| 14    | out                                                                                                                                          |
| 14    | in                                                                                                                                           |
| 14    | at                                                                                                                                           |
| 13    | this                                                                                                                                         |
| 12    | you                                                                                                                                          |
| 12    | on                                                                                                                                           |
| 12    | Perk                                                                                                                                         |
| 12    | #python                                                                                                                                      |
| 11    | it                                                                                                                                           |
| 10    | video                                                                                                                                        |
| 10    | really                                                                                                                                       |
| 10    | Learning                                                                                                                                     |
| 9     | ever.                                                                                                                                        |
| 8     | time:                                                                                                                                        |
| 8     | parrot                                                                                                                                       |
| 8     | montypyton                                                                                                                                   |
| 8     | dead                                                                                                                                         |
| 8     | cbcarts                                                                                                                                      |
| 8     | Raw                                                                                                                                          |
| 8     | PHP                                                                                                                                          |
| 8     | A                                                                                                                                            |
| 8     | #montypyton                                                                                                                                  |
| 8     | #cbcarts                                                                                                                                     |
| 8     | #Python                                                                                                                                      |
| 7     | was                                                                                                                                          |
| 7     | http://t.c…                                                                                                                                  |
| 7     | http://t.co/FdJmhOjpi2                                                                                                                       |
| 7     | can                                                                                                                                          |
| 7     | Ruby                                                                                                                                         |
| 7     | Python:                                                                                                                                      |
| 7     | O2                                                                                                                                           |
| 7     | Live                                                                                                                                         |
| 7     | Check                                                                                                                                        |
| 6     | we                                                                                                                                           |
| 6     | some                                                                                                                                         |
| 6     | que                                                                                                                                          |
| 6     | programming                                                                                                                                  |
| 6     | performs                                                                                                                                     |
| 6     | or                                                                                                                                           |
| 6     | have                                                                                                                                         |
| 6     | ever                                                                                                                                         |
| 6     | an                                                                                                                                           |
| 6     | Tac                                                                                                                                          |
| 6     | RT @CBCArts: The parrot is really dead this time: Raw video from the last #montypyton show ever. #cbcarts http://t.co/FdJmhOjpi2 http://t.c… |
| 6     | London's                                                                                                                                     |
| 6     | CBCArts                                                                                                                                      |
| 6     | Arena                                                                                                                                        |
| 6     | @CBCArts:                                                                                                                                    |
| 5     | your                                                                                                                                         |
| 5     | y                                                                                                                                            |
| 5     | what                                                                                                                                         |
| 5     | via                                                                                                                                          |
| 5     | that                                                                                                                                         |
| 5     | our                                                                                                                                          |
| 5     | no                                                                                                                                           |
| 5     | my                                                                                                                                           |
| 5     | monty                                                                                                                                        |
| 5     | http://t.co/xx6b6KPb27                                                                                                                       |
| 5     | http://t.co/m30gXhS9SH                                                                                                                       |
| 5     | en                                                                                                                                           |
| 5     | cinema                                                                                                                                       |
| 5     | be                                                                                                                                           |
| 5     | RT @CBCNews: Monty Python performs last show ever at London's O2 Arena http://t.co/xx6b6KPb27 http://t.co/m30gXhS9SH                         |
| 5     | Learn                                                                                                                                        |
| 5     | Developer                                                                                                                                    |
| 5     | CBCNews                                                                                                                                      |
| 5     | @CBCNews:                                                                                                                                    |
| 5     | 2:                                                                                                                                           |
| 4     | very                                                                                                                                         |
| 4     | una                                                                                                                                          |
| 4     | polyvore                                                                                                                                     |
| 4     | new                                                                                                                                          |
| 4     | need                                                                                                                                         |
| 4     | me                                                                                                                                           |
| 4     | just                                                                                                                                         |
| 4     | invertir                                                                                                                                     |
| 4     | http://t.co/o0SiMNUQIL                                                                                                                       |
| 4     | http://t.co/hjg6boEfVD                                                                                                                       |
| 4     | http://t.co/ebuB0ZlPHT                                                                                                                       |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------+

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值