Search code examples
pythonpython-3.xrequeststock

Possibility to change URL by result i'm looking for?


There is following code

import requests

# replace the "demo" apikey below with your own key from https://www.alphavantage.co/support/#api-key
url = 'https://www.alphavantage.co/query?function=EMA&symbol=IBM&interval=weekly&time_period=10&series_type=open&apikey=demo'
r = requests.get(url)
data = r.json()

print(data)

what i'm looking for is how to embed by request the stock symbol without changing all the time the URL code. Something like at the beginning

stock = input("Please enter a ticker symbol")

whereby stock equals IBM within the URL in this example

I thought about an approach like this but unfortunately its not working. In result i just get as a result "{}". Any idea?

import requests
# replace the "demo" apikey below with your own key from https://www.alphavantage.co/support/#api-key
stock = input("Please enter a ticker symbol")
url= f"https://www.alphavantage.co/query?function=EMA&symbol={'stock'}&interval=weekly&time_period=10&series_type=open&apikey=MYAPI"
r = requests.get(url)
data = r.json()

print(data)

PS if you have any better economic data from the web, i'm thankful for any advice.


Solution

  • Simply when you use f-string in your program you should not use a quotation mark inside the bracket

    my_text = 'text'
    f"{'my_text'}" # Wrong
    
    my_text = 'text'
    f"{my_text}" # True