Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tweepy流返回ImportError:无法导入名称或类型_Python_Mongodb_Tweepy - Fatal编程技术网

Python Tweepy流返回ImportError:无法导入名称或类型

Python Tweepy流返回ImportError:无法导入名称或类型,python,mongodb,tweepy,Python,Mongodb,Tweepy,我用tweepy拉推推,然后在MongoDB上显示它们。几天后,当我去重新运行它时,我得到了一个错误。 Mongodb运行良好,我的代码中甚至没有RE_类型 这是我的密码: import json import pymongo from tweepy import Stream from tweepy import OAuthHandler from tweepy.streaming import StreamListener CONSUMER_KEY = 'xxx'#hiding keys

我用tweepy拉推推,然后在MongoDB上显示它们。几天后,当我去重新运行它时,我得到了一个错误。 Mongodb运行良好,我的代码中甚至没有RE_类型

这是我的密码:

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)
导入json
进口pymongo
从tweepy导入流
从tweepy导入OAuthHandler
从tweepy.streaming导入StreamListener
消费者密钥='xxx'#隐藏密钥
消费者机密='xxx'
OAUTH_令牌='xxx'
OAUTH_TOKEN_SECRET='xxx'
关键字列表=['christmas']
类MyStreamListener(StreamListener):
定义初始化(自):
self.num_tweets=0#cpunter从0开始
self.tweet_coll=None#表示某些内容将在某个阶段进入tweet_coll
def mongo_连接(自):
尝试:
client=pymongo.MongoClient()
打印“Mongo已连接!”
db=client.tech_tweetsDB
self.tweet_coll=db.tweets
除了pymongo.errors.ConnectionFailure,e:
打印“无法连接到MongoB:%s”%e
def on_数据(自身、数据):
尝试:
#读推特
status=json.load(数据)
打印json.dumps(状态,缩进=4)
tweet={}
tweet[“text”]=状态['text']。编码('utf8')#自动阅读其他语言
tweet['screen\u name']=状态['user']['screen\u name']
tweet['followers\u count']=状态['user']['followers\u count']
tweet['friends\u count']=状态['user']['friends\u count']
tweet['favorite_count']=状态['favorite_count']
tweet['retweet\u count']=状态['retweet\u count']
tweet['created at']=状态['created_at']
打印状态。获取(“实体”)。获取(“介质”)
如果status.get(“实体”).get(“媒体”):
打印状态。获取(“实体”)。获取(“介质”)
媒体=状态['entities'][“媒体”]
tweet['media']=media[0][“display_url”]
其他:
tweet['media']=无
tweet['lang']=状态['user']['lang']
tweet['location']=状态['user']['location']
self.num_tweets+=1
打印self.num_推文
如果self.num_tweets<50:
#将tweet插入到集合中
self.tweet\u coll.insert(tweet)
返回真值
其他:
返回错误
返回真值
除BaseException作为e外:
打印(_数据失败:%s'%str(e))
返回真值
def on_错误(自身、状态):
打印(状态)
返回真值
auth=OAuthHandler(使用者密钥,使用者密钥)
身份验证设置访问令牌(OAUTH令牌、OAUTH令牌、机密)
#api=tweepy.api(auth)
stream=MyStreamListener()
stream.mongo_connect()
twitter\u stream=stream(auth,stream)
twitter\u stream.filter(track=关键字\u列表)
我的错误是:

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
回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\JetBrains\PyCharm 4.5.4\helpers\profiler\run\u profiler.py”,第146行,在中
profiler.run(文件)
文件“C:\Program Files(x86)\JetBrains\PyCharm 4.5.4\helpers\profiler\run\u profiler.py”,第85行,运行中
pydev_imports.execfile(file,globals,globals)#执行脚本
文件“C:/Users/Andrew/PycharmProjects/mongoDB/firstMongo.py”,第64行,在
进口pymongo
文件“C:\Users\Andrew\flask\u test\lib\site packages\pymongo\\uuuuuu init\uuuuuu.py”,第83行,在
从pymongo.collection导入返回文档
文件“C:\Users\Andrew\flask\u test\lib\site packages\pymongo\collection.py”,第34行,在
从pymongo.cursor导入游标
文件“C:\Users\Andrew\flask\u test\lib\site packages\pymongo\cursor.py”,第22行,在
从bson导入RE_类型
ImportError:无法导入名称或类型
进程已完成,退出代码为1
自pymongo 3.2以来,此问题已得到解决,只需通过pip升级您的软件包即可:

pip install pymongo --upgrade
自pymongo 3.2以来,此问题已得到修复,只需通过pip升级您的软件包即可:

pip install pymongo --upgrade

不幸的是有同样的问题。感谢您的回复。@AndrewCostello在最坏的情况下,卸载并重新安装它应该可以正常工作,不幸的是,同样的问题。感谢您的回复。@AndrewCostello在最坏的情况下卸载并重新安装应该可以工作