首页 > 代码库 > Python 收集Twitter时间序列数据

Python 收集Twitter时间序列数据

CODE:

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

'''
Created on 2014-7-18
@author: guaguastd
@name: collect_time_series.py
'''

if __name__ == '__main__':

    # import json
    import json
    
    # import partial
    from functools import partial
    
    # import trend
    from trend import twitter_trends

    # import time_series
    from time_series import get_time_series_data
        
    # 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()
    
    # sample usage 
    WORLD_WOE_ID = 1
    pp = partial(json.dumps, indent=1)
    twitter_world_trends = partial(twitter_trends, twitter_api, WORLD_WOE_ID)

    # collect time series
    get_time_series_data(twitter_world_trends, 'time-series', 'twitter_world_trends')

RESULT:

data: 
[{u'locations': [{u'woeid': 1, u'name': u'Worldwide'}], u'created_at': u'2014-07-17T22:46:34Z', u'_id': ObjectId('53c852dcae6f221648bfdde9'), u'trends': [{u'url': u'http://twitter.com/search?q=%23MH17', u'query': u'%23MH17', u'name': u'#MH17', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23GazzeSiyonizmeMezarOlacak', u'query': u'%23GazzeSiyonizmeMezarOlacak', u'name': u'#GazzeSiyonizmeMezarOlacak', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23PrayForMH17', u'query': u'%23PrayForMH17', u'name': u'#PrayForMH17', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23LouisWeLoveYou', u'query': u'%23LouisWeLoveYou', u'name': u'#LouisWeLoveYou', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23SpamIsraelinTurkey', u'query': u'%23SpamIsraelinTurkey', u'name': u'#SpamIsraelinTurkey', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22MuhsinBa%C5%9Fkan+Erdo%C4%9Fan%C4%B1Desteklerdi%22', u'query': u'%22MuhsinBa%C5%9Fkan+Erdo%C4%9Fan%C4%B1Desteklerdi%22', u'name': u'MuhsinBa\u015fkan Erdo\u011fan\u0131Desteklerdi', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22SoyunKurusun+Katilisrail%22', u'query': u'%22SoyunKurusun+Katilisrail%22', u'name': u'SoyunKurusun Katilisrail', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22IsraelIsSlaughtering+TheworldIsWatching%22', u'query': u'%22IsraelIsSlaughtering+TheworldIsWatching%22', u'name': u'IsraelIsSlaughtering TheworldIsWatching', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22Elaine+Stritch%22', u'query': u'%22Elaine+Stritch%22', u'name': u'Elaine Stritch', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22%C5%9EimdiDuaVakti+GazzeYan%C4%B1yor%22', u'query': u'%22%C5%9EimdiDuaVakti+GazzeYan%C4%B1yor%22', u'name': u'\u015eimdiDuaVakti GazzeYan\u0131yor', u'promoted_content': None}], u'as_of': u'2014-07-17T22:49:01Z'}]

Write 1 trends
Zzz...
data: 
[{u'locations': [{u'woeid': 1, u'name': u'Worldwide'}], u'created_at': u'2014-07-17T22:46:34Z', u'_id': ObjectId('53c8531eae6f221648bfddea'), u'trends': [{u'url': u'http://twitter.com/search?q=%23MH17', u'query': u'%23MH17', u'name': u'#MH17', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23GazzeSiyonizmeMezarOlacak', u'query': u'%23GazzeSiyonizmeMezarOlacak', u'name': u'#GazzeSiyonizmeMezarOlacak', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23PrayForMH17', u'query': u'%23PrayForMH17', u'name': u'#PrayForMH17', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23LouisWeLoveYou', u'query': u'%23LouisWeLoveYou', u'name': u'#LouisWeLoveYou', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23SpamIsraelinTurkey', u'query': u'%23SpamIsraelinTurkey', u'name': u'#SpamIsraelinTurkey', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22MuhsinBa%C5%9Fkan+Erdo%C4%9Fan%C4%B1Desteklerdi%22', u'query': u'%22MuhsinBa%C5%9Fkan+Erdo%C4%9Fan%C4%B1Desteklerdi%22', u'name': u'MuhsinBa\u015fkan Erdo\u011fan\u0131Desteklerdi', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22SoyunKurusun+Katilisrail%22', u'query': u'%22SoyunKurusun+Katilisrail%22', u'name': u'SoyunKurusun Katilisrail', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22IsraelIsSlaughtering+TheworldIsWatching%22', u'query': u'%22IsraelIsSlaughtering+TheworldIsWatching%22', u'name': u'IsraelIsSlaughtering TheworldIsWatching', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22Elaine+Stritch%22', u'query': u'%22Elaine+Stritch%22', u'name': u'Elaine Stritch', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22%C5%9EimdiDuaVakti+GazzeYan%C4%B1yor%22', u'query': u'%22%C5%9EimdiDuaVakti+GazzeYan%C4%B1yor%22', u'name': u'\u015eimdiDuaVakti GazzeYan\u0131yor', u'promoted_content': None}], u'as_of': u'2014-07-17T22:50:08Z'}]
Write 1 trends
Zzz...

data: 
[{u'locations': [{u'woeid': 1, u'name': u'Worldwide'}], u'created_at': u'2014-07-17T22:46:34Z', u'_id': ObjectId('53c85361ae6f221648bfddeb'), u'trends': [{u'url': u'http://twitter.com/search?q=%23MH17', u'query': u'%23MH17', u'name': u'#MH17', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23GazzeSiyonizmeMezarOlacak', u'query': u'%23GazzeSiyonizmeMezarOlacak', u'name': u'#GazzeSiyonizmeMezarOlacak', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23PrayForMH17', u'query': u'%23PrayForMH17', u'name': u'#PrayForMH17', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23LouisWeLoveYou', u'query': u'%23LouisWeLoveYou', u'name': u'#LouisWeLoveYou', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%23SpamIsraelinTurkey', u'query': u'%23SpamIsraelinTurkey', u'name': u'#SpamIsraelinTurkey', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22MuhsinBa%C5%9Fkan+Erdo%C4%9Fan%C4%B1Desteklerdi%22', u'query': u'%22MuhsinBa%C5%9Fkan+Erdo%C4%9Fan%C4%B1Desteklerdi%22', u'name': u'MuhsinBa\u015fkan Erdo\u011fan\u0131Desteklerdi', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22SoyunKurusun+Katilisrail%22', u'query': u'%22SoyunKurusun+Katilisrail%22', u'name': u'SoyunKurusun Katilisrail', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22IsraelIsSlaughtering+TheworldIsWatching%22', u'query': u'%22IsraelIsSlaughtering+TheworldIsWatching%22', u'name': u'IsraelIsSlaughtering TheworldIsWatching', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22Elaine+Stritch%22', u'query': u'%22Elaine+Stritch%22', u'name': u'Elaine Stritch', u'promoted_content': None}, {u'url': u'http://twitter.com/search?q=%22%C5%9EimdiDuaVakti+GazzeYan%C4%B1yor%22', u'query': u'%22%C5%9EimdiDuaVakti+GazzeYan%C4%B1yor%22', u'name': u'\u015eimdiDuaVakti GazzeYan\u0131yor', u'promoted_content': None}], u'as_of': u'2014-07-17T22:51:15Z'}]

Write 1 trends
Zzz...