Python Forum
Collecting Data from Twitter with Tweepy and MongoDB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Collecting Data from Twitter with Tweepy and MongoDB
#1
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.

[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
Reply
#2
solution found! i had to add tabs and to insert some brackets etc.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I scrape profile information from Twitter People search results? asdad 0 728 Nov-29-2022, 10:25 AM
Last Post: asdad
  swapping data in mongodb saisankalpj 0 730 Aug-30-2022, 12:42 PM
Last Post: saisankalpj
  Is it possible to write a python script to block twitter feeds? cubangt 0 866 Apr-07-2022, 04:14 PM
Last Post: cubangt
Exclamation MongoDB cannot connect - pymongo speedev 1 2,083 Aug-21-2021, 01:35 PM
Last Post: ndc85430
Bug Djongo (mongoDb) - Django rest framework Problem with search on JsonFields grzybekrafal 0 2,463 May-11-2021, 09:04 AM
Last Post: grzybekrafal
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 51,156 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
  Check if tweet is favorited by myself | TWEEPY altayko 0 1,361 Sep-16-2020, 05:40 PM
Last Post: altayko
  Retrieve data from MongoDB dsuk 2 2,282 Aug-24-2020, 09:22 PM
Last Post: micseydel
  find a string in a field in MongoDB Leon79 2 2,409 Jul-19-2020, 09:20 PM
Last Post: menator01
  insert more data at once in MongoDB Leon79 2 2,339 Jul-05-2020, 01:34 PM
Last Post: Leon79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020