我使用tweepy来发送推文,然后在MongoDB上显示它们。几天后,当我重新运行时,我收到了一个错误。 Mongodb工作正常,RE_TYPE甚至不在我的代码中。
这是我的代码:
import json
import pymongo
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
CONSUMER_KEY = 'xxx'#hiding keys
CONSUMER_SECRET = 'xxx'
OAUTH_TOKEN = 'xxx'
OAUTH_TOKEN_SECRET = 'xxx'
keyword_list = ['christmas']
class MyStreamListener(StreamListener):
def __init__(self):
self.num_tweets = 0 # cpunter starting at 0
self.tweet_coll = None # signal that something will go into tweet_coll at some stage
def mongo_connect(self):
try:
client = pymongo.MongoClient()
print "Mongo is connected!"
db = client.tech_tweetsDB
self.tweet_coll = db.tweets
except pymongo.errors.ConnectionFailure, e:
print "Could not connect to MongoB: %s" % e
def on_data(self, data):
try:
# read in a tweet
status = json.loads(data)
print json.dumps(status, indent=4)
tweet = {}
tweet["text"] = status['text'].encode('utf8') # autf able to read other languages
tweet['screen_name'] = status['user']['screen_name']
tweet['followers_count'] = status['user']['followers_count']
tweet['friends_count'] = status['user']['friends_count']
tweet['favorite_count'] = status['favorite_count']
tweet['retweet_count'] = status['retweet_count']
tweet['created at'] = status['created_at']
print status.get('entities').get("media")
if status.get('entities').get("media"):
print status.get('entities').get("media")
media = status['entities']["media"]
tweet['media'] = media[0]["display_url"]
else:
tweet['media'] = None
tweet['lang'] = status['user']['lang']
tweet['location'] = status['user']['location']
self.num_tweets += 1
print self.num_tweets
if self.num_tweets < 50:
# Insert tweet in to the collection
self.tweet_coll.insert(tweet)
return True
else:
return False
return True
except BaseException as e:
print('Failed on_data: %s' % str(e))
return True
def on_error(self, status):
print(status)
return True
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
# api = tweepy.API(auth)
stream = MyStreamListener()
stream.mongo_connect()
twitter_stream = Stream(auth, stream)
twitter_stream.filter(track=keyword_list)
我的错误是:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\profiler\run_profiler.py", line 146, in <module>
profiler.run(file)
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\profiler\run_profiler.py", line 85, in run
pydev_imports.execfile(file, globals, globals) # execute the script
File "C:/Users/Andrew/PycharmProjects/mongoDB/firstMongo.py", line 64, in <module>
import pymongo
File "C:\Users\Andrew\flask_test\lib\site-packages\pymongo\__init__.py", line 83, in <module>
from pymongo.collection import ReturnDocument
File "C:\Users\Andrew\flask_test\lib\site-packages\pymongo\collection.py", line 34, in <module>
from pymongo.cursor import Cursor
File "C:\Users\Andrew\flask_test\lib\site-packages\pymongo\cursor.py", line 22, in <module>
from bson import RE_TYPE
ImportError: cannot import name RE_TYPE
Process finished with exit code 1