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
Related articles
- python - i want to change the data obtained from the twitter api to a dictionary type
- i want to get the timeline of multiple users with twitter api
- python - get the follower's screen name with the twitter api
- emoji counting using twitter api in python
- i want to get twitter retweet information with python
- about instantiation with qpushbutton in python qt produces the same thing
Related questions
- python : List of dictionaries?
- javascript : How can I get the hidden site API
- How to write script via API in Python?
- How to translate into a normal format -a date from numbers. Python
- Hello, how can I bypass recaptcha v2 on a website via python, then login to the website and create cookies?
- You need to add a hyperlink and data from api for telegram bot in python
The cause is
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.