Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为请求API负载创建变量字符串_Python 3.x_String_Request_Python Requests_Payload - Fatal编程技术网

Python 3.x 为请求API负载创建变量字符串

Python 3.x 为请求API负载创建变量字符串,python-3.x,string,request,python-requests,payload,Python 3.x,String,Request,Python Requests,Payload,我有一个URL列表,所有这些URL都指向图像。我想在列表中循环并调用一个接受这些URL的。要调用API,我需要提供有效负载字典。但是,API中的示例代码要求有效负载字典采用以下形式: payload = "{\"url\":\"https://inferdo.com/img/face-3.jpg\",\"accuracy_boost\":3}" 此示例有效负载字典中的URL在我的列表中如下所示: list_of_urls = ["https://inferdo.com/img/face-3.j

我有一个URL列表,所有这些URL都指向图像。我想在列表中循环并调用一个接受这些URL的。要调用API,我需要提供有效负载字典。但是,API中的示例代码要求有效负载字典采用以下形式:

payload = "{\"url\":\"https://inferdo.com/img/face-3.jpg\",\"accuracy_boost\":3}"
此示例有效负载字典中的URL在我的列表中如下所示:

list_of_urls = ["https://inferdo.com/img/face-3.jpg", ...]
如何使用for循环将列表中的条目插入有效负载字典

我尝试使用“常规”负载字典,但它不起作用:

for url_path in list_of_urls:
    payload = {'url' : url_path,'accuracy_boost':3}

我查看了API文档,发现您需要将负载作为JSON发送。像这样的东西可以完成任务:

导入请求
导入json
端点={
“脸”:https://face-detection6.p.rapidapi.com/img/face'
“脸、年龄、性别”:https://face-detection6.p.rapidapi.com/img/face-age-gender'
}
URL=[
'https://inferdo.com/img/face-3.jpg'
]
标题={
“x-rapidapi-host”:“face-detection6.p.rapidapi.com”,
“x-rapidapi-key”:“YOUR-API-key”,
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”
}
对于url中的url:
有效载荷={
“url”:url,
“精度提升”:3
}
r=请求数.post(
endpoints.get('face')、#或endpoint.get('face_age_gender'))
data=json.dumps(有效负载),
标题=标题
)
如果r.ok:
#使用r.content或r.json()执行某些操作
我希望有帮助