根据用户inpu编码URL

2024-10-03 17:23:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python有点陌生,正在开发API。我想返回一个URI,它接受我的2个参数,这是输入(target\u group\u 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

这应该传递我的url中的参数,它需要如下所示:https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=19&date=20在传递日期和目标组id之后,2017年7月,但是我得到的是https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=%25s&date=%25s7220%20July%202017。我怎样才能解决这个问题?有没有代码样本可以帮助??谢谢


Tags: httpsselfidurltargetgetdateby
2条回答

你可以试试这个:

>>> 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'

我强烈建议您考虑使用requests library(“HTTP for humans”),它是专门为解决这类问题而设计的,比单独使用标准库更容易。你知道吗

给予

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)

这将正确地完成所有棘手的事情,并避免您必须解决以前多次解决过的问题(即使对于棘手的问题)。你知道吗

相关问题 更多 >