我有一个SQL Server数据库,我可以使用它连接和查询pyodbc。我的select语句拉出一个容器(在一家货运公司工作),我想使用API将该容器传递给URL,以从端口获取其数据。我的问题是如何将选择的结果存储为变量并将其传递给URL?
我当前的代码是:
cursor.execute ('''SELECT container from container_table ''')
rows = cursor.fetchall()
for row in rows:
print(row.container)
head = {"X-Session-Token":'xxxxxxxxx'}
ash = (
'https://pilot.ports.co.il/WebAPI/api/ContainerStatus/ContainerNumber=**row.container**
getash = requests.get(ash, headers=head)
print(getash.status_code)
print(getash.json())
如果我了解您要正确执行的操作,则可以在Python 3.6及更高版本中使用f-string:
cursor.execute ('''SELECT container from container_table ''')
rows = cursor.fetchall()
for row in rows:
print(row.container)
head = {"X-Session-Token":'xxxxxxxxx'}
ash = (f"https://pilot.ports.co.il/WebAPI/api/ContainerStatus/ContainerNumber={row.container}")
getash = requests.get(ash, headers=head)
print(getash.status_code)
print(getash.json())