I want to get tweets from a program using Tweepy.streaming in a timeline.

Asked 2 years ago, Updated 2 years ago, 96 views

Program Contents

#-*-coding:utf-8-*-

from tweet.streaming import StreamListener
from tweet import OAuthHandler
from tweet import Stream

consumer_key="# Write the consumer_key information in quotation marks
consumer_secret="# Include consumer_secret information in quotation marks

access_token="# Write access_token information in quotation marks
access_token_secret="# Write access_token_secret information in quotation marks

class StdOutListener (StreamListener):
    defon_data(self,data):
        if data.startwith("{"):
            print data
        return True

    default_error(self, status):
        print status

if__name__=='__main__':
    l=StdOutListener()
    auth=OAuthHandler(consumer_key,consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream=Stream(auth,l)
    stream.filter(track=[u"Japan"))#To search
    if 'stream' in stream:
        print stream ['text'].encode('utf-8')
#    stream.sample()# To get random sampling of tweets
#    stream.userstream()#To retrieve the timeline

Run Results

[root@localhost Desktop] #python intento.py
{"created_at":"Thu Sep 22 08:01:36 +0000 2016","id":778866827346714624,"id_str":"778866827346714624","text":"RT @RealOviedo: \u00a1Buenos d\u00edas, oviedistas! S\u00ed, hoy jugamos en casa (20h) #RealOviedoReus https:\/\/t.co\/qKRRBF2eot","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":714885097,"id_str":"714885097","name":"Gorkitta","screen_name":"gorka_laporte","location":null,"url":null,"description":"Zu zara nagusia.\nREAL OVIEDO \u2764\u2764 ATHLETIC CLUB","protected": false, "verified": false, "followers_count": 368, "friends_count": 278, "listed_count":11, "favours_count": 2643, "status_count": 15851, "created_at": "Tue Juul 24 20:30:59+002012", "troughs_count": 12600:"

I would like the above program to be displayed in text, but it doesn't work.
For example, @saksa Japan is the best! and I would like it to be displayed in text.
If you know how to do it, I would appreciate it if you could let me know.
Thank you for your cooperation.

python twitter

2022-09-30 19:11

1 Answers

Is there more than one data data format for on_data?If you want to react only when JSON comes, you can do the following:

import json
defon_data(self,data):
    try:
       payload=json.loads(data)
       print payload.get('text')
       return True
    except:
       pass
    return False


2022-09-30 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.