Tweepy API出错

时间:2017-01-19 21:20:56

标签: python tweepy

我是使用Tweepy的新手,我刚刚试图从youtube教程中执行这行代码,但不断收到错误。有谁知道发生了什么?这是我的代码

import tweepy
from tweepy import OAuthHandler

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

res = api.search(q="IPython")

Errror:

  

Traceback(最近一次调用最后一次):文件   “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tweepy/binder.py”   第186行,执行中       auth = auth,UnboundLocalError:赋值前引用的局部变量'auth'

在处理上述异常期间,发生了另一个异常:

  

Traceback(最近一次调用最后一次):文件   “/Users/christopherdimitrisastropranoto/Desktop/twitterAnalysis/Listener.py”   第22行,在       trending = trial.trends_place(1)文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tweepy/binder.py”,   第245行,在_call中       return method.execute()文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tweepy/binder.py”,   第189行,执行中       提出TweepError('发送请求失败:%s'%e)tweepy.error.TweepError:发送请求失败:本地变量'auth'   在分配前引用

1 个答案:

答案 0 :(得分:0)

这是您进行任何类型的API请求之前应具备的基本设置结构:

import tweepy

consumer_key = "X-U" #this you get when you make create an application on twitter as a dev
consumer_secret = "Z" #this you get when you make create an application on twitter as a dev

acess_token = "Y-V" #this you get when you make create an application on twitter as a dev
acess_token_secret = "P" #this you get when you make create an application on twitter as a dev


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(acess_token, acess_token_secret)

api = tweepy.API(auth, wait_on_rate_limit=True)

然后您可以尝试以下操作:

tweet = api.search(q="IPython")

然后,如果要打印推文的文本:

t = tweet.text
print(t)