我试图将特定的字符串注入请求中,以创建易于使用的API。这都是出于学习目的。
我正在使用python和jpyter笔记本。下面的命令有效,但是我希望能够通过某种方式即时修改字符串注入。
url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=AMD&interval=5min&apikey=('alpha.txt')"
import requests
##this is the data I want to inject
atest = {'TIME_SERIES_INTRADAY&symbol=AMD&interval=5min'}
##New request attempting to use injection
url = "https://www.alphavantage.co/query?function={atest}&apikey=('alpha.txt')"
r = requests.get(url)
text = r.text
此命令的最终结果应显示amd的json股票数据
答案 0 :(得分:0)
尝试使用.format
import requests
##this is the data I want to inject
atest = {'TIME_SERIES_INTRADAY&symbol=AMD&interval=5min'}
##New request attempting to use injection
url = "https://www.alphavantage.co/query?function={0}&apikey=('alpha.txt')"
url = url.format(atest)
r = requests.get(url)
text = r.text
您可以将多个参数(字符串)传递给.format函数。