TweepError: Failed to parse JSON payload: Expecting value or ']': line 1 column 689339 (char 689338)
TweepError: Failed to parse JSON payload: Expecting value or ']': line 1 column 689339 (char 689338)
我正在尝试从某些用户时间轴获取推文并将其保存到 csv 文件,但是当我 运行 我的代码出现此错误 TweepError:无法解析 JSON 负载:预期值或“]”:第 1 行第 689339 列(字符 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)`
我认为这是阿拉伯语推文的问题,对吗?
在构建二维数组的行中,将 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]
然后验证您的 Code/Console 编码。我尝试了该脚本,它在 Atom 上完美运行。
我正在尝试从某些用户时间轴获取推文并将其保存到 csv 文件,但是当我 运行 我的代码出现此错误 TweepError:无法解析 JSON 负载:预期值或“]”:第 1 行第 689339 列(字符 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)`
我认为这是阿拉伯语推文的问题,对吗?
在构建二维数组的行中,将 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]
然后验证您的 Code/Console 编码。我尝试了该脚本,它在 Atom 上完美运行。