Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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/1/cocoa/3.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 requests.get中输入变量?_Python_Python Requests - Fatal编程技术网

如何转动“开关”;。获得;在Python requests.get中输入变量?

如何转动“开关”;。获得;在Python requests.get中输入变量?,python,python-requests,Python,Python Requests,我不熟悉用Python编程。我想为Python请求方法(.get、.post等)设置一个变量 类似于: import requests method = 'get' url = 'https://google.com' response = requests.method(url) 这当然会导致错误,因为请求没有方法选项 感觉答案取决于getattr,但我不知道要传递哪些参数getattr 有什么建议吗 谢谢您可以使用以下代码: 导入请求 get=requests.get post=req

我不熟悉用Python编程。我想为Python
请求
方法(
.get
.post
等)设置一个变量

类似于:

import requests

method = 'get'
url = 'https://google.com'

response = requests.method(url)
这当然会导致错误,因为
请求
没有
方法
选项

感觉答案取决于
getattr
,但我不知道要传递哪些参数
getattr

有什么建议吗


谢谢

您可以使用以下代码:

导入请求
get=requests.get
post=requests.post
put=requests.put
patch=requests.patch
delete=requests.delete
url='1〕https://google.com'
google=get(url)
如果希望将其作为类,可以使用以下代码:

导入请求
类方法:
定义初始化(自):
self.get=requests.get
self.post=requests.post
self.put=requests.put
self.patch=requests.patch
self.delete=requests.delete
方法=方法()
url='1〕https://google.com'
方法=“获取”
google=getattr(方法,方法)(url)

这是很有价值的,确实有用,谢谢。虽然我想知道如何/是否有可能将点符号选项变成变量。检查这是否提供了更多信息@SeaDude补充道,这对解决实际问题毫无帮助。@AbhigyanJaiswal OP正在寻求一种方法来执行作为字符串变量的任意方法。这是一项更适合的工作。即非常方便。非常感谢。