如何使用Python根据用户输入触发获取请求

时间:2019-02-16 07:57:59

标签: python pandas python-requests

我将获得以下get请求的输出

[u'Mango', u'Banana', u'Apple', u'Grapes']

我会要求用户选择一种水果

fruit_name = raw_input("Enter fruit name from the above list :")

一旦我从用户那里得到输入,我就希望按照下面的用户输入发出开火请求...

如果fruit_name是芒果

执行以下获取请求

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

如果fruit_name是香蕉

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

如果fruit_name是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

有人可以帮助我实现这一目标吗?

2 个答案:

答案 0 :(得分:2)

如果每个列表中的不同值更好,则可以使用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'}

fruit_name = raw_input("Enter fruit name from the above list :")

out = d.get(fruit_name, 'No value in list')
print out

Enter fruit name from the above list :Banana
http://10.111.21.314:8000/api/datasources/proxy/1/query

Enter fruit name from the above list :test
No value in list

答案 1 :(得分:1)

加上@jezrael的答案,您可以执行以下操作:

L = [u'Mango', u'Banana', u'Apple', u'Grapes']

fruit_name = raw_input("Enter fruit name from the above list :")
url_dict = {'Mango': 'http://12.345.67.891:8000/api/datasources/proxy/1/query',
            'Banana': 'http://10.111.21.314:8000/api/datasources/proxy/1/query',
            'Apple': 'http://15.161.71.819:8000/api/datasources/proxy/1/query',
            'Grapes': 'http://20.212.22.324:8000/api/datasources/proxy/1/query'}
print url_dict.get(fruit_name, 'No URL configured')