question

leopold avatar image
1 Like"
leopold asked ·

How to Access Screen Results in Python

I am using the screener in Python. The result seems to be a list of nested dictionaries. But when I checked the type, each item in the list is actually a "intrinio_sdk.models.security_screen_result.SecurityScreenResult". It looks like a dictionary, but I can't access information using the regular method.


So how can I access information in "intrinio_sdk.models.security_screen_result.SecurityScreenResult"?


Thanks.

pythonscreener
· 5
10 |600 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks for the reply.

My question is about how to access the result from the screener.

The API sends back a list of items which look like nested dictionaries. But these "dictionaries" can't be accessed using normal methods.

When I use this:

api_response[1]['data']

I get this error message:

TypeError: 'SecurityScreenResult' object is not subscriptable
0 Likes 0 · ·

Basically, I want to know how I can access information in SecurityScreenResult (https://github.com/intrinio/python-sdk/blob/master/docs/SecurityScreenResultData.md ). What is this exactly? Not JSON, not a list, not a dictionary.

1 Like 1 · ·

Hi Leopold,

I had a similar problem when I was trying to create an output of the json file.
I seemed to have solved it by using jsonpickle

Before creating a json.dump, I had to encode it such as -

jsonpickle.encode(api_response)

I don't know if that helps you?

0 Likes 0 · ·
Show more comments

1 Answer

· Write an Answer
asrinand avatar image
0 Likes"
asrinand answered ·

Hi @leopold,

I guess ultimately in the end you are trying to parse the json file, and possible write it into a csv/excel file?

I figured out how to do this, but I don't use the intrinio python sdk mainly because as you already pointed out, the SecurityScreenResult is not a list or a dictionary so hard to read.

Even if you were to use jsonpickle, it's difficult to extract the info. Accessing the api using just the url works pretty well for me now and you can find the script I use and hopefully it will help you out.

The following script queries to get the price to earnings ratio of less than equal to 25 and total equity greater than or equal to $1 Billion -

import requests
import json
import csv
import urllib.request as ur
import urllib.parse as par

url = "https://api.intrinio.com/securities/search?conditions=pricetoearnings~lte~25,totalequity~gte~1000000000&api_key=your_api_key"
otfile = '/home/stock_output.csv' #the directory where you want the file to be created

html = ur.urlopen(url).read()
data = json.loads(html.decode('utf-8'))
x = data['data']

with open(otfile, 'w') as csvfile:
        headers = ["ticker","pricetoearnings","totalequity"]
        writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)
        writer.writeheader()
        for i in x:
            writer.writerow({'ticker': i['ticker'], 'pricetoearnings': i['pricetoearnings'], 'totalequity': i['totalequity']})
· 1 · Share
10 |600 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks.

But if converting to a CSV and loading it into python is the only way, Intrinio really need to improve their Python API.

0 Likes 0 · ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.