Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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
如何在提取twitter推文的python脚本中解决此错误_Python_Python 3.x_Python 2.7_Twitter - Fatal编程技术网

如何在提取twitter推文的python脚本中解决此错误

如何在提取twitter推文的python脚本中解决此错误,python,python-3.x,python-2.7,twitter,Python,Python 3.x,Python 2.7,Twitter,我使用的是python3,但这里的代码是针对python2的。我在运行代码时出错。我必须从推特上获取推特。 我已经安装了tweepy,但仍然出现错误。 我的访问密钥、使用者密钥和使用者密钥是正确的。我怎样才能解决这个问题。是否有其他解决此问题的方法 import tweepy #https://github.com/tweepy/tweepy import csv #Twitter API credentials consumer_key = "xxxx" consumer_secret =

我使用的是python3,但这里的代码是针对python2的。我在运行代码时出错。我必须从推特上获取推特。 我已经安装了tweepy,但仍然出现错误。 我的访问密钥、使用者密钥和使用者密钥是正确的。我怎样才能解决这个问题。是否有其他解决此问题的方法

import tweepy #https://github.com/tweepy/tweepy
import csv

#Twitter API credentials
consumer_key = "xxxx"
consumer_secret = "xxxx"
access_key = "xxxx"
access_secret = "xxxx"


def get_all_tweets(screen_name):
#Twitter only allows access to a users most recent 3240 tweets with this method

#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

#initialize a list to hold all the tweepy Tweets
alltweets = []

#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name,count=200)

#save most recent tweets
alltweets.extend(new_tweets)

#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
print ("getting tweets before %s" % (oldest))

#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)`enter code here`

#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

print("...%s tweets downloaded so far" % (len(alltweets)))

#transform the tweepy tweets into a 2D array that will populate the csv 
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
#write the csv
with open('%s_tweets.csv' % screen_name, 'wb') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)


pass


if __name__ == '__main__':
#pass in the username of the account you want to download
get_all_tweets("ArsalanBajwa")

'


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-e2a778291283> in <module>()
     21 
     22 #make initial request for most recent tweets (200 is the maximum allowed count)
---> 23 new_tweets = api.user_timeline(screen_name,count=200)
     24 
     25 #save most recent tweets

NameError: name 'api' is not defined

how to resolve it?
导入tweepy#https://github.com/tweepy/tweepy
导入csv
#Twitter API凭据
消费者_key=“xxxx”
消费者_secret=“xxxx”
访问\u key=“xxxx”
访问\u secret=“xxxx”
def获取所有推文(屏幕名称):
#Twitter仅允许使用此方法访问用户最近的3240条推文
#授权twitter,初始化tweepy
auth=tweepy.OAuthHandler(使用者密钥,使用者密钥)
授权设置访问令牌(访问密钥、访问密钥)
api=tweepy.api(auth)
#初始化一个列表以保存所有tweepy Tweets
所有tweets=[]
#对最近的推文发出初始请求(200是允许的最大数量)
新tweets=api.user\u时间线(屏幕名称,计数=200)
#保存最近的推文
alltweets.extend(新推特)
#保存最早的tweet的id
最早的=所有tweets[-1]。id-1
#继续抓取推文,直到没有可抓取的推文
而len(新推特)>0:
打印(“在%s之前获取推文”%(最早))
#所有子请求都使用max_id参数来防止重复
新tweets=api.user\u时间线(screen\u name=screen\u name,count=200,max\u id=oldest)
#保存最近的推文
alltweets.extend(new_tweets)`在这里输入代码`
#更新最早的tweet的id
最早的=所有tweets[-1]。id-1
打印(“…%s条推文已下载”%(len(alltweets)))
#将tweepy tweets转换为二维数组,以填充csv
outtweets=[[tweet.id_str,tweet.created_at,tweet.text.encode(“utf-8”)]用于所有tweets中的tweet]
#编写csv
将open(“%s_tweets.csv”%screen_name,'wb')作为f:
writer=csv.writer(f)
writer.writerow([“id”、“created_at”、“text”])
writer.writerows(outtweets)
通过
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#传入要下载的帐户的用户名
获取所有推文(“ArsalanBajwa”)
'
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
21
22#对最近的推文发出初始请求(200是允许的最大数量)
--->23新推文=api.user\u时间线(屏幕名称,计数=200)
24
25#保存最近的推文
NameError:未定义名称“api”
如何解决?

试着用大写字母书写,我遇到了这个错误,我就是这样修复的


new_tweets=API.user_timeline(screen_name,count=200)

您的代码是否没有识别,或者只是在复制代码时才取消缩进?