使用我从this tutorial获得的代码,我正在尝试从Python创建twitter bot,以转发使用某些关键字找到的任何推文。它使用来自Python-Twitter Developers和Redis的python-twitter API包装器。我已经安装并获得了包装器和Redis,它们运行时没有任何问题,但是当我从命令行运行twit_bot.py时,我收到以下错误:
Traceback (most recent call last):
File "twit_bot.py", line 116, in <module>
twitter_session = auth_to_twitter(API_CREDENTIALS)
File "twit_bot.py", line 27, in auth_to_twitter
api = twitter.Api(consumer_key=api_credentials['consumer_key'],
AttributeError: 'module' object has no attribute 'Api'
This is a pastebin with my bot.我是一个完整的python newb,真的不知道如何解决这个问题。
从上面的pastebin我已经从第27行开始复制到第36行:
def auth_to_twitter(api_credentials):
"""Returns an authenticated twitter session object"""
api = twitter.Api(consumer_key=api_credentials['consumer_key'],
consumer_secret=api_credentials['consumer_secret'],
access_token_key=api_credentials['access_token_key'],
access_token_secret=api_credentials['access_token_secret'])
if (api.VerifyCredentials() is not None):
return api
return False
更新:新错误
在运行pip freeze
并卸载我在路径中的额外twitter包装器后,正如用户Bibhas推荐的那样,我从命令行运行 twit_bot.py ,我仍然收到错误,这个时间看起来如下:
Traceback (most recent call last):
File "twit_bot.py", line 118, in <module>
run_bot(twitter_session, redis, TWITTERBOT_KEYWORDS)
File "twit_bot.py", line 108, in run_bot
update_search_stack(api_session, redis, key)
File "twit_bot.py", line 65, in update_search_stack
since_id = get_since_id(redis, "%s:%s" % (TWITTERBOT_LIST, keyword))
File "twit_bot.py", line 41, in get_since_id
since_id = redis.get(key + ":last_since_id")
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/client.py", line 585, in get
return self.execute_command('GET', name)
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/client.py", line 381, in execute_command
connection.send_command(*args)
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/connection.py", line 304, in send_command
self.send_packed_command(self.pack_command(*args))
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/connection.py", line 286, in send_packed_command
self.connect()
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/redis/connection.py", line 234, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 61 connecting localhost:6379. Connection refused.
再次更新!
由于Redis服务器看起来像是给我一个问题(
redis.exceptions.ConnectionError: Error 61 connecting localhost:6379. Connection refused.
),如上面的错误所示,我调查this stackoverflow question希望得到一个潜在的答案,但进入python控制台和运行
>>> import redis
>>> print redis.Redis("localhost")
<redis.client.Redis object at 0x10e5480d0>
>>>
给了我一些没用的东西,因为它说它已连接(我想!?!?!)所以我仍然不确定如何解决这个问题以便我的机器人工作。 :C
def run_bot(api_session, redis, keywords):
""""""
while 42:
for key in keywords:
update_search_stack(api_session, redis, key)
update_since_id(redis, "%s:%s" % (TWITTERBOT_LIST, key))
tweet_and_shout(api_session, redis, key, timeout=1)
print "Waiting for one minute\n\n\n"
sleep(60)
if __name__ == "__main__":
twitter_session = auth_to_twitter(API_CREDENTIALS)
redis = redis.Redis("localhost")
run_bot(twitter_session, redis, TWITTERBOT_KEYWORDS)