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

时间:2018-01-08 20:55:06

标签: python json python-3.x tweepy

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

  

文件“”,第63行,在getTweetsList中       json.dump(status._json,file,sort_keys = True,indent = 4)

     

文件“C:\ ProgramData \ Anaconda3 \ lib \ json__init __。py”,第180行,在   转储

     

fp.write(块)

     

TypeError:需要类似字节的对象,而不是'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

我到处寻找答案,但没有一个解决方案适合我。我认为它可能与Python 3.6有关。

2 个答案:

答案 0 :(得分:0)

  • 将行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格式的字符串
  • "转储"表示"转储字符串"

答案 1 :(得分:0)

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

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

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