存在密钥时出现 KeyError

问题描述

使用 Tweepy,我试图实时流式传输推文并将其保存到数据库 sqlite 中,但是一旦我加载 JSON 文件并将其存储到数据库中,错误就会不断发生,说 KeyError : created_at 但已经存在一个密钥created_at。我也和其他人一起试过,但错误仍然存​​在。

这是我的代码

import sqlite3
conn = sqlite3.connect("twitter.db")
import sys
import tweepy
import json
tb_create = "CREATE TABLE tweets2(created_at,favorite_count,favorited,filter_level,lang,retweet_count,retweeted,source,text,truncated,user_created_at,user_followers_count,user_location,user_lang,user_name,user_screen_name,user_time_zone,user_utc_offset,user_friends_count)"
c = conn.cursor()
c.execute(tb_create)
conn.commit

import tweepy

class MaxListener(tweepy.StreamListener):
  def on_data(self,raw_data):
    all_data = json.loads(raw_data)

    created_at = all_data['created_at']
    favorite_count = all_data["favorite_count"]
    favorited = all_data["favorited"]
    filter_level = all_data["filter_level"]
    lang = all_data["lang"]
    retweet_count = all_data["retweet_count"]
    retweeted = all_data["retweeted"]
    source = all_data["source"]
    text = all_data["text"]
    truncated = all_data["truncated"]
    user_created_at = all_data["user"]["created_at"]
    user_followers_count = all_data["user"]["followers_count"]
    user_location = all_data["user"]["location"]
    user_lang = all_data["user"]["lang"]
    user_name = all_data["user"]["name"]
    user_screen_name = all_data["user"]["screen_name"]
    user_time_zone = all_data["user"]["time_zone"]
    user_utc_offset = all_data["user"]["utc_offset"]
    user_friends_count = all_data["user"]["friends_count"]

    c.execute("INSERT INTO tweets2(created_at,user_friends_count) VALUES (?,?,?)",(created_at,user_friends_count))

    conn.commit()

  def process_data(delf,raw_data):
    print(raw_data)

  def on_error(self,status_code):
    if status_code == 420:
      return False

class MaxStream():
  def __init__(self,auth,listner):
    self.stream = tweepy.Stream(auth=auth,listener=listener)

  def start(self,keyword_list):
    self.stream.filter(track= keyword_list)

if __name__ == "__main__":
  listener = MaxListener()
  from tweepy import OAuthHandler
  auth = OAuthHandler(ckey,csecret)
  auth.set_access_token(atoken,asecret)

  stream = MaxStream(auth,listener)
  stream.start("python")

错误

KeyError                                  Traceback (most recent call last)
<ipython-input-4-0bf7c95f9c6c> in <module>()
     55 
     56   stream = MaxStream(auth,listener)
---> 57   stream.start("python")

8 frames
<ipython-input-4-0bf7c95f9c6c> in on_data(self,raw_data)
     10     all_data = json.loads(raw_data)
     11 
---> 12     created_at = all_data['created_at']
     13     favorite_count = all_data["favorite_count"]
     14     favorited = all_data["favorited"]

KeyError: 'created_at'

解决方法

on_data 接收来自流的所有数据,包括不是推文负载的 message types

当收到其中一种其他消息类型时,几乎肯定会发生 KeyError

在将其用作推文之前,您应该检查是否存在特定于推文的属性,或者考虑改用 on_status