0

I am pulling stock data from an REST API, however the API doesn't supply a list of stocks they offer so my list and their list is different. I running my requests through nested loops and I'm trying to figure out how to except KeyError just move on to the next company in the list.

for stock in stock_list:
    #queryString = "symbols:"+stock+" AND publishedAt:[2021-03-20 TO 2021-04-02]"
    queryString =  "source.id:sec-api AND symbols:" + stock

    payload = {
        "type": "filterArticles",
        "queryString": queryString,
        "from": 0,
        "size": 2000
    }
    print(queryString)
    # Format your payload to JSON bytes
    jsondata = json.dumps(payload)
    jsondataasbytes = jsondata.encode('utf-8')

    # Instantiate the request
    req = urllib.request.Request(API_ENDPOINT)

    # Set the correct HTTP header: Content-Type = application/json
    req.add_header('Content-Type', 'application/json; charset=utf-8')
    # Set the correct length of your request
    req.add_header('Content-Length', len(jsondataasbytes))

    # Send the request to the API
    response = urllib.request.urlopen(req, jsondataasbytes)

    # Read the response
    res_body = response.read()

    # Transform the response into JSON
    assets = json.loads(res_body.decode("utf-8"))

    ##### parse JSON Array into variables for SQL ######
    articles = assets["articles"][0:50]
    #print(articles)
    for article in articles:
        title = (article['title'][0:50])        
        stock_id =(article['id'][0:50])
        date = (article['publishedAt'][0:50])
        sources = (article['source'])
        name = (sources['name'][0:50])
        id = (sources['id'][0:50])
        #print(title)    
        details = (article['details'])
        company_name = (details['name'][0:50])
        type = (details['type'][0:50])
        #print(type)
        cik = (details['cik'][0:50])
        symbol = (details['ticker'][0:50])
        link_sec = (details['linkToHtmlAnnouncement'][0:50])
        description = (details['description'][0:50])

        try:
            cursor.execute("""
                INSERT INTO stock_sec (stock_id, title, date, description, link_sec, type, symbol, company_name, cik) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) 
        """,(stock_id[0:50], title[0:50], date[0:50], description[0:50], link_sec[0:50], type[0:50], symbol[0:50], company_name[0:50], cik[0:50]))
        except Exception as e:
            print(e)   
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Where are you getting `KeyError`? – Barmar Apr 15 '21 at 20:43
  • And if you use `[0:50]` when setting `date`, you don't need it again when calling `cursor.execute()` – Barmar Apr 15 '21 at 20:44
  • 1
    Don't use `id` and `type` as variable names, they're the names of built-in functions. – Barmar Apr 15 '21 at 20:46
  • You can simply do `if "articles" in assets:` – Barmar Apr 15 '21 at 20:47
  • Or `articles = assets.get("articles", [])[0:50]` – Barmar Apr 15 '21 at 20:49
  • Does [Ignore KeyError and continue program](https://stackoverflow.com/questions/15653966/ignore-keyerror-and-continue-program) answer your question? If your code produces an Exception, always include the complete Traceback - copy and paste it then format it as code (select it and type `ctrl-k`) – wwii Apr 15 '21 at 20:50
  • details = (article['details']) is where the error is triggered but only for certain symbols – Zachary Holwerda Apr 15 '21 at 20:56

0 Answers0