向ann URL添加2个不同的参数并返回一个自定义响应

2024-10-03 17:26:57 发布

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

我对python还是新手, 我想返回一个URI,它接受我的两个参数,它们是输入(target\u group\u id,date),这是我的基本url,get\u customer\u action\u by\u target\u group\u 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=202017年7月在通过日期和目标组id之后,但是我得到了这个相当https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=%25s&date=%25s7220%20July%202017。我怎样才能解决这个问题?有没有代码样本可以帮助??谢谢


Tags: httpsselfidurltargetgetdateby
1条回答
网友
1楼 · 发布于 2024-10-03 17:26:57

我假设您的基本字符串是https://api4.optimove.net/current/customers/GetCustomerActionsByTargetGroup?targetGroupID=%25s&date=%25s,那么您的代码应该是

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

然后pyton将用self.TargetGroupID和self.date中的值替换原始链接中%s的两个值

此外,部分20%20July%202017与预期一样,因为在URI中,空间转义为%20,所以它表示2017年7月20日

相关问题 更多 >