我试图从某些用户时间线获取推文并将其保存到csv文件,但是当我运行我的代码时,我收到此错误TweepError:无法解析JSON有效负载:期望值或' ]':第1行第689339行(char 689338) 对于某些用户而言,我不知道原因,但我该如何解决此错误以及为什么会出现此错误?
这是我的代码
def get_all_tweets(screen_name):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
alltweets = []
#tweets_mention = []
new_tweets = api.user_timeline(screen_name = 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=5000,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
#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] for tweet in alltweets]
#write the csv
with open('%s_tweets.csv' % screen_name, 'w', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)`
答案 0 :(得分:1)
我认为阿拉伯语推文有问题吗?
在构建2D数组的行中,将encode("utf-8")
添加到tweet.text
该行将为:
#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]
然后验证您的代码/控制台编码。我尝试了脚本,它在Atom上完美运行。