我在python中有点新功能并且在使用API。我想返回一个接受我的2个参数的URI,它是输入的(target_group_id,date),这是我的基本URL
get_customer_action_by_target_group_url = \
'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=&date='
这是我的功能。
def get_customer_action_by_target_group(self):
payload = {"TargetGroupID": "%s" % self.TargetGroupID, "Date":"%s" % self.date,
}
if not self.TargetGroupID or not self.date:
get_target_group_id = (raw_input("Please provide the target Group id:"))
get_date = (raw_input("Please provide the date as required:"))
self.TargetGroupID = get_target_group_id
self.date = get_date
response = self.send_request(self.get_customer_action_by_target_group_url + self.TargetGroupID +
self.date,
json.dumps(payload), "GET")
print response, response.text, response.reason
return response
这应该通过我的网址中的参数,这需要看起来像这样:https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=19&date=20 2017年7月传递日期和目标groupe_id之后,但我得到的却是https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=%25s&date=%25s7220%20July%202017。我怎么能解决这个问题?任何可以帮助的代码示例?谢谢
答案 0 :(得分:0)
我强烈建议您考虑使用requests library(“人类的HTTP”),它专门用于解决此类问题,而不仅仅是标准库。
鉴于
url = 'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup'
tgid = 19
date = '20 July 2017'
您可以使用
执行所需的HTTP请求data = {'TargetGroupID': tgid, 'date': date}
requests.get(url, params=data)
这将正确地完成所有棘手的事情并且避免你必须多次解决已经解决的问题(即使对于粗糙的角落情况)。
答案 1 :(得分:-1)
你可以试试这个:
>>> get_customer_action_by_target_group_url = 'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID={0}&date={1}'
>>>
>>> targetGroupID=19
>>> date="20 july 2017"
>>>
>>> get_customer_action_by_target_group_url.format(targetGroupID, date)
'https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=19&date=20 july 2017'