Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.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替换Get请求中的用户输入值_Python_Pandas_Python Requests - Fatal编程技术网

如何使用Python替换Get请求中的用户输入值

如何使用Python替换Get请求中的用户输入值,python,pandas,python-requests,Python,Pandas,Python Requests,URL在单个变量中定义,并将根据用户的输入在get请求中使用,请查找下面的代码 d = {'Mango': 'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20

URL在单个变量中定义,并将根据用户的输入在get请求中使用,请查找下面的代码

d = {'Mango': 'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fsoak-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms',
 'Banana':'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fspike-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms',
 'Apple':'http://12.345.67.891:8000/api/datasources/proxy/1/query?db=UK_GHS&q=SELECT%20sum(%22count%22)%20FROM%20%22gatling%22%20WHERE%20%22status%22%20%3D%20%27ok%27%20AND%20%22simulation%22%20%3D~%20%2Fload-test*%2F%20AND%20time%20%3E%201544491800000ms%20and%20time%20%3C%201544495400000ms%20GROUP%20BY%20%22script%22&epoch=ms'}

Dashboard_name = raw_input("Enter dashboard name from the above list :")
如果用户输入芒果,get请求将取第一个URL,如果用户输入香蕉,get请求将取第二个URL,依此类推 我实现了您建议的代码,但由于URL不是直接的,而且它是根据用户输入的,所以出现以下错误

    final_url = d.format(user_input_to_epoch_time(user_input_from),
AttributeError: 'dict' object has no attribute 'format'
代码如下:

import time

SHORT_URL_TEMPLATE_MANGO = 'http://12.345.67.891:8000/api/mango?from={}&to={}'
SHORT_URL_TEMPLATE_APPLE = 'http://12.345.67.891:8000/api/apple?from={}&to={}'

URLS = {'mango': SHORT_URL_TEMPLATE_MANGO, 'apple': 
        SHORT_URL_TEMPLATE_APPLE}


def user_input_to_epoch_time(user_input):
    return int(time.mktime(time.strptime(user_input, '%Y-%m-%d %H:%M:%S')))


dash_board = 'mango'
user_input_from = "2018-10-12 23:12:44"
user_input_to = "2018-10-12 23:17:55"

final_url = URLS[dash_board].format(user_input_to_epoch_time(user_input_from),
                                user_input_to_epoch_time(user_input_to))

print(final_url)  
输出:

http://12.345.67.891:8000/api/mango?from=1539375164&to=1539375475

您可以查看可能的重复情况,这不会替换URL中的查询上面的代码帮助我将人工时间转换为unix时间,但如何将taht unix时间存储在变量中并替换为get请求URL中的时间?代码已修改,并显示了如何替换URL中的时间字段,感谢它工作正常。但在我的代码中,URL是以不同的方式定义的。我用更多的细节编辑了代码。你能看一下吗?我不确定我是否遵守了。。你所需要做的(据我所知)就是根据你的需要修改简短的URL模板。还剩下什么吗?请解释一下
http://12.345.67.891:8000/api/mango?from=1539375164&to=1539375475