Question

I want to see all the tweets that have a specific hashtag. I wrote the code like this:

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

ckey = 'xxx'
csecret = 'xxx'
atoken = 'xxx'
asecret = 'xxx'

class listener(StreamListener):

    def on_status(self, status):
        print 'Tweet text: ' + status.text

        for hashtag in status.entries['hashtags']:
            print hashtag['text']

        return True

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(follow=[23], track=["#django"])

Follow a number of my friends, track - wants to see the messages with the hashtag.

On a test account that I wrote constantly follow the hashtag who wants to see. When I run the program, python crashes.

What I'm doing wrong?

Thanks for help.

Was it helpful?

Solution

try something like this:
   def on_data(self, data):

        jsonData=json.loads(data)
        text=jsonData['text']
        text2=jsonData['entities']['hashtags']
        for hashtag in text2:
             text2=hashtag['text']
        print text+str(text2)
        return True

    def on_error(self, status):
        print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter( track=["django"])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top