在 Python 3.6 中将推文保存到 JSON 文件

Saving tweets to JSON file in Python 3.6

我正在使用 tweepy 库下载某些用户的推文。我想将这些推文保存到 JSON 文件中,但我收到以下错误:

File "", line 63, in getTweetsList json.dump(status._json,file,sort_keys = True,indent = 4)

File "C:\ProgramData\Anaconda3\lib\json__init__.py", line 180, in dump

fp.write(chunk)

TypeError: a bytes-like object is required, not 'str'

代码如下:

def getTweetsList(self, screen_name):
    # Twitter only allows access to a users most recent 3240 tweets with this method
    # initialize a list to hold all the tweepy Tweets
    alltweets = []  

    # make initial request for most recent tweets (200 is the maximum allowed count)
    new_tweets = self.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:  
        # all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = self.api.user_timeline(screen_name = screen_name,count=200,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)))

    print("Total tweets downloaded %s" % (len(alltweets)))
    file = open('tweet.json', 'wb') 
    print("Writing tweet objects to JSON please wait...")
    for status in alltweets:
        json.dump(status._json,file,sort_keys = True,indent = 4)
    return alltweets

我到处寻找答案,但 none 的解决方案对我有用。我认为这可能与 Python 3.6.

有关
  • 将行 json.dump(status._json,file,sort_keys = True,indent = 4) 更改为 json.dumps(status._json,file,sort_keys = True,indent = 4)
  • 使用json.dumps
  • json.dumps 将对象序列化为 JSON 格式的字符串
  • "dumps" 表示 "dump string"

您正在以二进制模式打开文件。该错误表明它因此需要一个字节对象(二进制数据)。推文是文本(Unicode 字符串)。使用文本模式并声明编码,例如:

with open('tweet.json', 'w', encoding='utf8') as file:
    json.dump(status._json, file, ...)

请注意,使用 with 语句可确保文件被关闭。