Hi everyone!
I am a beginner in python programing language and my first project is to build a script which collects twitter tweets with specific pattern.
The pattern combinations are: 'ECB' and 'exit' , 'ECB' and 'taper' , 'ECB' and 'tapering' , 'Draghi' and 'taper' , 'Draghi' and 'tapering' , 'Draghi' and 'exit'
Example for first one what it should mean: Search for tweets which contains the two words "ECB" AND "EXIT".
I have the latest tweepy package installed (v3.6.0) and i am working with jetbrains and python3. MongoDB is already installed and server is running on localhost.
The problem: Python tells me that stdoutlistener is not defined with a name error although it is imported.
best regards
fatony
I am a beginner in python programing language and my first project is to build a script which collects twitter tweets with specific pattern.
The pattern combinations are: 'ECB' and 'exit' , 'ECB' and 'taper' , 'ECB' and 'tapering' , 'Draghi' and 'taper' , 'Draghi' and 'tapering' , 'Draghi' and 'exit'
Example for first one what it should mean: Search for tweets which contains the two words "ECB" AND "EXIT".
I have the latest tweepy package installed (v3.6.0) and i am working with jetbrains and python3. MongoDB is already installed and server is running on localhost.
The problem: Python tells me that stdoutlistener is not defined with a name error although it is imported.
[color=#333333][font=Courier New]from tweepy.streaming import StreamListener
[/font][/color]
from tweepy import OAuthHandler
from tweepy import Stream
import json
access_token = "CENSORED"
access_token_secret = "CENSORED"
consumer_key = "CENSORED"
consumer_secret = "CENSORED"
# Definition of the Listener Class for getting streaming StdOutListener(StreamListener):
def on_data(self, ecbdata):
# Abruf der Details wie ID, getwitterter Text und wo es erstellt worden ist.
tweet = json.loads(ecbdata)
created_at = tweet["created_at"]
id_str = tweet["id_str"]
text = tweet["text"]
coordinates = tweet["coordinates"]
obj = {"created_at": created_at, "id_str": id_str, "text": text, }
tweetind = collection.insert_one(obj).inserted_id
print (obj)
return True
def on_error(self, status):
print (status)
if __name__ == '__main__':
#Control of twitter authentification and connecting to twitter streaming api
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
# Connection to MongoDB Server on localhost
from pymongo import MongoClient
client = MongoClient()
client = MongoClient('localhost', 27017)
db = client['twitter_ecb']
collection = db['twitter_ecb']
# Searching for tweets
[color=#333333][font=Courier New] stream.filter(track=['ECB' and 'exit' , 'ECB' and 'taper' , 'ECB' and 'tapering' , 'Draghi' and 'taper' , 'Draghi' and 'tapering' , 'Draghi' and 'exit'])[/font][/color]What shall i do?best regards
fatony
