Home>

I tried to execute the following code to get the timeline from twitter in python,
It didn't work.
I wondered if I couldn't create the class that inherits StreamListener because the Listener isn't defined, but I'm not quite sure about StreamListener. Is the syntax wrong? Or is there something I should do before writing the syntax?
Could you tell me the cause of the error and the StreamListener?
Referenced sites →Link content
Referenced code ↓

from datetime import timedelta
CK ='xxxx'
CS ='xxxx'
AT ='xxxx'
AS ='xxxx'
class Listener(tweepy.StreamListener):
    def on_status(self, status):
        status.created_at += timedelta(hours=9)
        print('------------------------------')
        print(status.text)
        print("{name}({screen}) {created} via {src}\n".format(name=status.author.name, screen=status.author.screen_name,created=status.created_at, src=status. source))
        return True
    def on_error(self, status_code):
        print('Error occurred:' + str(status_code))
        return True
    def on_timeout(self):
        print('Timeout...')
        return True
auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, AS)
listener = Listener()
stream = tweepy.Stream(auth, listener)
stream.filter(track = ["programming"])


Code and error when trying to execute ↓

import tweepy
>>>from datetime import timedelta
>>>CK =''
>>>CS =''
>>>AT = ``
>>>AS =''
>>>class Listener(tweepy.StreamListener):
...def on_status(self, status):
...status.created_at += timedelta(hours=9)
... print('------------------------------')
... print(status.text)
... print("{name}({screen}) {created} via {src}\n".format(name=status.author.name, screen=status.author.screen_name,created=status.created_at, src =status.source))
... return True
...def on_error(self, status_code):
... print('Error occurred:' + str(status_code))
... return True
...def on_timeout(self):
... print('Timeout...')
... return True
... auth = tweepy.OAuthHandler(CK, CS)
  File "<stdin>", line 14
    auth = tweepy.OAuthHandler(CK, CS)
    ^
SyntaxError: invalid syntax
>>>auth = tweepy.OAuthHandler(CK, CS)
>>>auth.set_access_token(AT, AS)
>>>listener = Listener()
Traceback (most recent call last):
  File "<stdin>", line 1, in<module>
NameError: name'Listener' is not defined
  • Answer # 1

    The cause is

    ... auth = tweepy.OAuthHandler(CK, CS)
      File "<stdin>", line 14
        auth = tweepy.OAuthHandler(CK, CS)
        ^
    SyntaxError: invalid syntax


    At the time of the error, as you can see from the fact that auth is preceded by "...", it was "in the middle" of the class definition input. Since I got an error, class is not defined either.

    If you keep doing this way, at the end of the class definition part, leave blank and end the continuous input (the line with "..." at the beginning), and after ">>>", auth = It is OK if you continue to input ....

    However, if it is this long, it is recommended to execute it as a file instead of the python prompt, or import it and use it.