2017-09-23 37 views
0

我试图从某些用户时间轴中获取tweet并将其保存到csv文件,但是当我运行我的代码我得到这个错误TweepError:无法解析JSON负载:期望值或']':行1列689339(字符689338) 它的工作原理,我不知道这个原因,但我怎么能解决这个错误,为什么这个错误出现?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

检查此解决方案:https://stackoverflow.com/a/41157563/5078746 – Fcmam5

+0

已经检查过它,它什么都不做 –

+0

在tweet底部添加.encode(“utf-8”)到tweet.text。 text.encode(“utf-8”)' – Fcmam5

回答

1

我认为这是一个问题,阿拉伯鸣叫吧?在你构造你的二维数组行 ,添加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上完美运行。

相关问题