可以按我正在寻找的结果更改 URL 吗?



代码

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)

我要找的是如何通过请求嵌入股票代码而不改变所有时间的URL代码。比如开头

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

在此示例中,URL中的stock等于IBM

我想过这样的方法,但不幸的是它不起作用。结果,我只是得到一个结果"{}"。任何想法?

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如果你从网上有更好的经济数据,我很感谢你的任何建议。

当你在程序中使用f-string时你不应该在括号内使用引号

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

做以下事情:

f"....{'stock'}...."

只是字符串连接的一个更花哨的版本。它基本上是用字符串代替变量stock

相同
"......"+'stock'+"....."

如果你删除了' ', python现在将获取它的值

f'.....{stock}.....' 

最新更新