在Twepy中在类StdOutListener中传递on_data函数中的参数

时间:2016-11-26 00:22:52

标签: python mongodb python-3.x twitter tweepy

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),但它不起作用。

0 个答案:

没有答案