keyerror

w9apscun  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(382)

使用tweepy,我试图实时流化tweets并将其保存到数据库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, 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) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", (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))

    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'
r7knjye2

r7knjye21#

on_data 接收来自流的所有数据,包括非tweet有效负载的消息类型。
这个 KeyError 几乎可以肯定的是,当接收到其他消息类型之一时会发生。
在使用推特之前,应检查是否存在特定于twitt的属性,或考虑使用 on_status 相反

相关问题