Skip to content
Advertisement

KeyError when there exists a key

Using Tweepy, I was trying to live stream the Tweets and save it into database sqlite but as soon as I load the JSON file and store into database the error keeps on occurring saying KeyError : created_at but there already exists a key created_at. I tried with others too, but still the error persists.

Here is my code:

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")

error

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'

Advertisement

Answer

on_data receives all data from the stream, including message types that aren’t Tweet payloads.

The KeyError is almost certainly occurring when one of those other message types is received.

You should check for the existence of an attribute specific to Tweets before using it as a Tweet or consider using on_status instead.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement