计算主题标签的推文

时间:2013-05-17 14:33:58

标签: python twitter-streaming-api tweetstream

我想知道是否有任何方法可以使用像hashtags.org这样的流媒体API来计算来自twitter的主题标签我已经使用python和tweetstream创建了一个脚本而且我可以计算但是对于TT总是180k我相信它限制为50条推文/秒。这是代码:

#!/usr/bin/python
import tweetstream
    import sys
    print sys.argv
    twitterUsername = "user"
    twitterPassword = "pass"
    twitterWordFilter = sys.argv[1]

    try:
        with tweetstream.FilterStream(twitterUsername, twitterPassword,track=twitterWordFilter) as stream:
            for tweet in stream:
                print stream.count

    except tweetstream.ConnectionError, e:
        print "Disconnected from twitter. Reason:", e.reason

1 个答案:

答案 0 :(得分:0)

def get_tweet_count(term):

    total_tweet_count = 0 
    page = 1
    while True:
        url = 'http://search.twitter.com/search.json?q=' 
                + urllib.quote(term) + '&rpp=100&page=' + str(page)

        response = urllib2.urlopen(url)
        json_content = response.read()
        tweets = json.loads(json_content)['results']
        total_tweet_count += len(tweets)

        # Are we at the last page or have we run out of pages?
        if len(tweets) < 100 or page >= 15:
            break

        max_id = tweets[0]['id_str']
        page += 1
        # Wait so twitter doesn't get annoyed with us
        time.sleep(1)

    return total_tweet_count

此脚本改编自code on GitHub.