-2

Im trying to ignore KeyError in my case or other kind of errors while working with dictionaries that have blank or "NA" values.

This is my code. The error comes from when using the stock like "CCIV", which does not have an employee count in Yahoo Finance. Also sometimes some companies have the board member pay as "NA".


# first attempt at something

import requests
from bs4 import BeautifulSoup
import json
import re
from io import StringIO

url_profile = 'https://finance.yahoo.com/quote/{}/profile?p={}'

stock = input("\n\tStock :\t")

# -------- NLP to stock ticker -------------


# Work in progress


# -------- NLP to stock ticker -------------


response = requests.get(url_profile.format(stock, stock))

soup = BeautifulSoup(response.text, 'html.parser')

pattern = re.compile(r'\s--\sData\s--\s')
script_data = soup.find('script', text = pattern).contents[0]

start = script_data.find('context')-2

json_data = json.loads(script_data[start:-12])


if ValueError or KeyError:
    pass

# ---- Ticker

symbol = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['symbol']


# ---- Employees

employees = str(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['fullTimeEmployees']


# ---- Titles
title = []
for i in range(3):

    title.append(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['companyOfficers'][i]['title'])

# ---- Names
name = []
for i in range(3):

        name.append(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['companyOfficers'][i]['name'])

# ---- Ages

age = []
for i in range(3):

        age.append(str(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['companyOfficers'][i]['age']))

# ---- Pay

pay = []
for i in range(3):

        pay.append(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['companyOfficers'][i]['totalPay']['longFmt'])


# ---- Formatting (cred. "Anon Coward")

rows = [
    ["Ticker :", symbol],
    ["Employees :", employees],
    ["Title :", title[0], title[1], title[2]],
    ["Name :", name[0], name[1], name[2]],
    ["Ages :", age[0], age[1], age[2]],
    ["Pay :", pay[0], pay[1], pay[2]],
]

# Build up a max length of each column
lengths = {}
for row in rows:
    for i in range(len(row)):
        lengths[i] = max(lengths.get(i, 0), len(row[i]))

for row in rows:
    # For each cell, padd it by the max length
    output = ""
    for i in range(len(row)):
        if len(output) > 0:
            # Add a space between columns
            output += "    "
        cell = row[i] + " " * lengths[i]
        cell = cell[:lengths[i]]
        output += cell
    print(output+"\n")

I have tried things like :

employees = 0

if str(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['fullTimeEmployees']) != KeyError:
    employees = str(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['fullTimeEmployees'])

But I still get the error. Help very much appreciated.

avocardio
  • 9
  • 1
  • 6
  • Use `.get(key)` instead of `[key]` if you want to return a default value (e.g. `None`) instead of raising `KeyError` when the key is not present. – Samwise Feb 18 '21 at 18:02

2 Answers2

0

KeyError exception is what is raised when you try to access a key that isn’t in a dictionary

you can use try/except

https://realpython.com/python-keyerror/

shoam
  • 155
  • 7
0

If you're looking for an easier way to get that data, you can try a package called yahooquery. DISCLAIMER: I'm the author of the package.

from yahooquery import Ticker

t = Ticker('CCIV')
t.company_officers
            maxAge                  name  age                    title  yearBorn  exercisedValue  unexercisedValue
symbol row                                                                                                        
CCIV   0         1  Mr. Michael S. Klein   56     Chairman, CEO & Pres      1964               0                 0
       1         1   Mr. Lee Jay Taragin   54  Chief Financial Officer      1966               0                 0
putty
  • 744
  • 1
  • 6
  • 14