是否可以根据我正在寻找的结果更改 URL?
Possibility to change URL by result i'm looking for?
有如下代码
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}.....'
有如下代码
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}.....'