Reputation: 101
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.
Upvotes: 1
Views: 105
Reputation:
Doing the following:
f"....{'stock'}...."
is just a little fancier version of string concatenation. It is basically substituting the string instead of the variable stock
.
It is the same as:
"......"+'stock'+"....."
If you remove the ' '
, python will now fetch its value
f'.....{stock}.....'
Upvotes: 1
Reputation: 779
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
Upvotes: 3