Python TweepError:未能分析JSON有效负载:应为值或“]”:第1行第689339列(字符689338)
我试图从一些用户的时间线中获取推文并将其保存到csv文件中,但当我运行代码时,我收到以下错误TweepError:未能解析JSON负载:期望值或']':第1行第689339列char 689338 对于一些用户来说,我不知道这是什么原因,但是我如何解决这个错误以及为什么会出现这个错误呢 这是我的密码Python TweepError:未能分析JSON有效负载:应为值或“]”:第1行第689339列(字符689338),python,json,csv,twitter,tweepy,Python,Json,Csv,Twitter,Tweepy,我试图从一些用户的时间线中获取推文并将其保存到csv文件中,但当我运行代码时,我收到以下错误TweepError:未能解析JSON负载:期望值或']':第1行第689339列char 689338 对于一些用户来说,我不知道这是什么原因,但是我如何解决这个错误以及为什么会出现这个错误呢 这是我的密码 def get_all_tweets(screen_name): auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.se
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)`
我认为这是阿拉伯语推文的问题,对吗? 在构建2D数组的行中,将encodeutf-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上运行得非常好。检查这个解决方案:已经检查过了,但它没有执行任何操作。在底线tweet.text.encodeutf-8中,将.encodeutf-8添加到tweet.text。代码工作正常,但文件中的数据无法读取。它有编码问题