Python:我可以将多个变量放入数据库API的URL中吗?
我正在尝试使用Python通过API访问数据库。我的代码的第一部分工作得很好。所以这个Python片段Python:我可以将多个变量放入数据库API的URL中吗?,python,api,variables,url,Python,Api,Variables,Url,我正在尝试使用Python通过API访问数据库。我的代码的第一部分工作得很好。所以这个Python片段 import requests import json url = 'http://api.sportradar.us/ncaafb-p1/2016/REG/schedule.json? api_key=5tpxxxxxxxxxxxxxxxxx' response = requests.get(url) json_data = response.json() for games in j
import requests
import json
url = 'http://api.sportradar.us/ncaafb-p1/2016/REG/schedule.json?
api_key=5tpxxxxxxxxxxxxxxxxx'
response = requests.get(url)
json_data = response.json()
for games in json_data['weeks'][0]['games']:
id = games.get('id')
hid = games.get('home')
vid = games.get('away')
print("hometeam ",hid, " visitor ",vid, " game id ",id)
…生成此输出:
hometeam CAL visitor HAW game id 1c4ee96d-3f08-43e1-8357-3bc2ee8bcaf8
hometeam NDS visitor CCH game id 6f5121d1-d813-4ba3-bdf3-badf6412fa4f
hometeam WAG visitor SAC game id 9a39c485-9930-4f70-8a16-59d9bc5e7822
hometeam MIZST visitor SWC game id 196a7570-18d9-41ae-baa6-57ad9a3abce0
.......
我现在要做的是将变量'hid'和'vid'作为变量插入到第二个url中,并再次调用数据库
curl = '"http://api.sportradar.us/ncaafb-p1/2016/REG/1/{the vid variable}/{the hid variable}/boxscore.json?api_key=5tpxxxxxxxxxxxxxxxxx"
cresponse = requests.get(curl)
x = cresponse.json()
print(x)
这只是语法问题吗?我试着这样做
curl = '"http://api.sportradar.us/ncaafb-p1/2016/REG/1/{s}/{s}/boxscore.json?api_key=5tpxxxxxxxxxxxxxxxxx".format(vid, hid)'
我试过这个:
curl = '"http://api.sportradar.us/ncaafb-p1/2016/REG/1/%s/%s/boxscore.json?api_key=5tpxxxxxxxxxxxxxxxxx"%(vid, hid)'
这是:
curl = '"http://api.sportradar.us/ncaafb-p1/2016/REG/1/%s/%s/boxscore.json?s=%s.ns&vid=%s&hid=%sapi_key=5tpxxxxxxxxxxxxxxxxx"%(ticker, vid, hid)
但我仍然收到各种错误消息。关于如何正确执行此操作(鉴于“hid”和“vid”是CAL和HAW等多字母组合)?我们可以看到的任何日志错误?引号的位置、位置和类型(单引号与双引号)似乎是导致问题的原因,请将
.format(vid,hid)
移到结束引号之外(后面),通常三重引用长字符串会使长引号更容易:curl=“”http://api.sportradar.us/ncaafb-p1/2016/REG/1/{the_vid_variable}/{the_hid_variable}/boxscore.json?api_key=5tpxxxxxxxxxxx”“。格式(the_vid_variable=vid,the_hid_variable=hid)