1

I have a newbie questions: let say I have this list of stock in python

import requests

list = ["AMZN","APPL", "BAC"]

try:
    for x in list:
        url ='https://financialmodelingprep.com/api/v3/quote-short/'+x+'?apikey=demo'
        response = requests.request('GET', url)
        result = response.json()
        print(result[0]["price"]) 

except:
    pass

the second ticker will throw an exceptions, how do I make python to run the third ticker no matter what happen to the second ticker requests?

1

2 Answers 2

1

Use try-except inside for loop like below

import requests

list = ["AMZN","APPL", "BAC"]


for x in list:
    try:
        url ='https://financialmodelingprep.com/api/v3/quote-short/'+x+'?apikey=demo'
        response = requests.request('GET', url)
        result = response.json()
        print(result[0]["price"]) 

    except:
       pass
Sign up to request clarification or add additional context in comments.

Comments

0

You can use continue

import requests

list = ["AMZN","APPL", "BAC"]

for x in list:
    try:
        url ='https://financialmodelingprep.com/api/v3/quote-short/'+x+'?apikey=demo'
        response = requests.request('GET', url)
        result = response.json()
        print(result[0]["price"])
    except:
        continue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.