名称错误 - 未定义名称api

时间:2016-06-08 07:40:20

标签: python-3.4 tweepy

我想在特定的时间范围内从twitter中提取推文。代码

import tweepy
import csv
from tweepy import OAuthHandler
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''

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

#csvFile=open('tweets.csv','a')
#csvWriter=csv.writer(csvFile)

for tweet in tweepy.Cursor(api.search,
                           q="#tatamotors",
                           lang="en",
                           since="2016-01-12",
                           until="2016-06-07").items():

    print (tweet.created_at, tweet.text)
    #csvWriter.writerow([tweet.created_at,tweet.text.encode('utf-8')])

我得到 - “名称错误:名称api未定义”

1 个答案:

答案 0 :(得分:1)

您在调用api之前错过了一行,首先使用此构建api

api = tweepy.API(auth)