代码之家  ›  专栏  ›  技术社区  ›  Charbel Hanna

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

  •  0
  • Charbel Hanna  · 技术社区  · 8 年前

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

    getTweetsList中第63行的文件“” json。转储(状态。\u json,file,sort\u keys=True,indent=4)

    文件“C:\ProgramData\Anaconda3\lib\json\uuu init\uuuuuuuuuu0.py”,第180行,in 倾倒

    fp。写入(区块)

    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 回复  |  直到 8 年前
        1
  •  2
  •   Mark Tolonen    8 年前

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

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

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

        2
  •  1
  •   Jai    8 年前
    • 更改线路 json.dump(status._json,file,sort_keys = True,indent = 4) json.dumps(status._json,file,sort_keys = True,indent = 4)
    • 使用 json.dumps
    • json。转储文件 将对象序列化为JSON格式的字符串
    • “转储”表示“转储字符串”