我正在尝试从Twitter的30天沙箱搜索API收集推文,并将它们写入csv文件。我收到错误代码403,但无法从错误消息中收集任何有用的信息。我看了相关的帖子,或者已经尝试过他们所说的解决问题的方法,或者他们正在尝试做完全不同的事情(错误代码403似乎是许多问题的统称错误代码)
我已经能够从标准搜索中收集推文,但我需要的数据比过去7到9天的可用数据还多。我在Twitter上有一个开发人员帐户,在我的仪表板上,它说在30天的搜索沙箱中,本月我仍然有250个请求和100万条推文(我用0)
下面是我的代码:
import csv
import settings as sett #file that includes my access token, consumer key and the secrets of each.
from TwitterAPI import TwitterAPI
api = TwitterAPI(sett.consumer_key, sett.consumer_secret, sett.access_token, sett.access_token_secret)
r = api.request('tweets/search/30day/:maruchan.json', {'query' : 'maruchan'})
#^.......^To be honest I don't know what is supposed to go here, I just put something random, this might be the problem....
csvFile = open('maruchan.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
for tweet in r:
try:
#Write a row to the csv file/ I use encode utf-8
csvWriter.writerow([tweet.created_at, tweet.full_text.encode('utf-8'), tweet.favorite_count, tweet.retweet_count, tweet.id, tweet.user.screen_name])
except tweepy.TweepError:
pass
csvFile.close()
当我在Jupyter笔记本中运行此程序时,出现以下错误:
---------------------------------------------------------------------------
TwitterRequestError Traceback (most recent call last)
<ipython-input-122-0fe9d55ba54b> in <module>
15 csvWriter = csv.writer(csvFile)
16
---> 17 for tweet in r:
18
19 try:
~\Anaconda3\lib\site-packages\TwitterAPI\TwitterAPI.py in __iter__(self)
217 :raises: TwitterConnectionError, TwitterRequestError
218 """
--> 219 return self.get_iterator()
220
221 def get_quota(self):
~\Anaconda3\lib\site-packages\TwitterAPI\TwitterAPI.py in get_iterator(self)
204 """
205 if self.response.status_code != 200:
--> 206 raise TwitterRequestError(self.response.status_code)
207
208 if self.stream:
TwitterRequestError: Twitter request failed (403)
我对编码非常陌生,不知道自己在做什么,但是我无法自己解决这个问题,因此,如果有人可以帮助我,我将不胜感激。谢谢!
答案 0 :(得分:1)
我弄清楚了我的问题所在。端点错误,并且csvWriter.writerow的输入也错误。
端点: 对于端点,您的环境名称是通过转到您的twitter开发者帐户找到的,在右上角有一个带有您的用户名的下拉菜单,单击“ Dev环境”。这将带您到每个开发环境的列表下,每个开发环境应为“开发环境标签”(在我的情况下,该标签为“研究”)。取标签并将其放入您的API请求中,并在其前面加上一个冒号,然后再添加“ .json”。注意:对于完整档案搜索,只需将“ 30day”替换为“ fullarchive”。
CSV输入: 对于CSV输入,它们必须采用tweet ['aspect']的格式,其中tweet是我们可以迭代的任意变量名称,而'aspect'是请求的数据类型(例如'text'或'created_at' )
下面是正确的代码以及一些注释。
import csv
import settings as sett #file that includes my access token, consumer key and the secrets of each. You get these by applying for a developer account and making an app
from TwitterAPI import TwitterAPI
api = TwitterAPI(sett.consumer_key, sett.consumer_secret, sett.access_token, sett.access_token_secret) #tells the Twitter API who you are.
r = api.request('tweets/search/30day/:research', {'query' : 'maruchan lang:en', #maruchan is the search term and lang:en filters for english results
"maxResults": "100", #number of tweets collected (starting from newer tweets)
"fromDate":"201906250000", #start data
"toDate":"201907010000" #end data (tweets will only be collected between these dates)
#YYYYMMDDHHmm #format of the fromDate and toDate input values
})
csvFile = open('maruchan_30_day_sandbox.csv', 'a') #'maruchan_30_day_sandbox.csv' is file name, 'a' is for append mode
#Use csv Writer
csvWriter = csv.writer(csvFile)
for tweet in r:
#creates csv file with the listed elements seperated by a comma in each row #encode('utf-8') helps read certain characters
csvWriter.writerow([tweet['created_at'], tweet['user']['screen_name'], tweet['text'].encode('utf-8') if 'text' in tweet else tweet])
csvFile.close()