import tweepy
import csv
from tweepy import Stream
from tweepy.streaming import StreamListener
import json
from pymongo import MongoClient
from tweepy import OAuthHandler
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
class StdOutListener(StreamListener):
#This function gets called every time a new tweet is received on the stream
def on_data(self, data):
print(the_argument)
Client = MongoClient()
db = Client["Twitter"]
collection = db[the_argument]
j=json.loads(data)
text=j["text"] #The text of the tweet
collection.insert({'tweet':text})
def on_error(self, status):
print("ERROR")
print(status)
twitter_stream = Stream(auth, StdOutListener())
twitter_stream.filter(track=['#'+'hashtag'], encoding='utf8')
在上面的代码中,在class StdOutListener
中,我想在函数the_argument
中的collection = db[the_argument]
中传递参数on_data
。我如何通过这个论点?我尝试了不同的方式来传递def on_data(self, data,the_argument)
,但它不起作用。