Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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 根据用户输入对URL进行编码_Python - Fatal编程技术网

Python 根据用户输入对URL进行编码

Python 根据用户输入对URL进行编码,python,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_cu

我对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中的参数,它需要如下所示:在传递日期和目标groupe_id后的2017年7月,但我得到的是这个。我怎样才能解决这个问题?任何有帮助的代码示例??谢谢你

你可以试试这个:

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

<>我强烈敦促你考虑使用HTTP给人类,这是专门用来解决这类问题比单独使用标准库更容易。

给定

您可以使用执行所需的HTTP请求

data = {'TargetGroupID': tgid, 'date': date}
requests.get(url, params=data)

这将正确地完成所有棘手的工作,避免您必须解决以前多次解决过的问题。

发送请求函数如何构建url的可能重复?您是否使用任何framewrok来实现此目的?还有一个建议:尝试应用。这意味着:不要将用户交互与其他功能混为一谈。在您的代码中,如果缺少target_group_id,而date不存在,或者相反,则将按要求使用来输入这两者。。。
data = {'TargetGroupID': tgid, 'date': date}
requests.get(url, params=data)