如何获取python请求的所有可能的输入参数

时间:2018-10-29 13:51:52

标签: python python-requests

我正在尝试查找python requests方法的文档,但看来这是一个隐藏方法。 Documentation

例如:当我这样做时:
response = requests("GET", URL, data=test, timeout=60)
在哪里可以获得所有可能的输入参数?

实际上,我什至找不到requests方法的文档,似乎它已被弃用并分成.get.post等。

1 个答案:

答案 0 :(得分:2)

如果不确定模块的作用,并且找不到文档(顺便说一下,您当然可以在requests上找到这些文档),则可以使用python dir函数:

dir(requests)

这将打印该对象下的所有属性(模块,函数,成员)。而且在python中,一切都是对象,因此您可以对任何未知(或已知)对象执行此操作。您可以进一步打印属性的类型:

print("\n".join(['{} : {}'.format(name, type(getattr(requests,name))) for name in dir(requests)]))

这是退货的切片

__path__ : <type 'list'>
__title__ : <type 'str'>
__version__ : <type 'str'>
adapters : <type 'module'>
api : <type 'module'>
auth : <type 'module'>
delete : <type 'function'>
exceptions : <type 'module'>
get : <type 'function'>

您可以通过requests.[name]致电其中的每一个,例如requests.get

如果要查找函数的参数,可以执行以下操作:

requests.get.__code__.co_varnames

请注意,**kwargs将注册为一个参数,并且几乎不可能在不进入代码或文档的情况下找到它的含义。

在这种情况下,某些功能已被详细记录。您可以执行func.__doc__。例如,requests.get调用requests.request,如果您进行requests.request.__doc__,则会有一个很好的文档来说明一切:

Constructs and sends a :class:`Request <Request>`.
Returns :class:`Response <Response>` object.

:param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': ('filename', fileobj)}``) for multipart encoding upload.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How long to wait for the server to send data
    before giving up, as a float, or a (`connect timeout, read timeout
    <user/advanced.html#timeouts>`_) tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.

Usage::

  >>> import requests
  >>> req = requests.request('GET', 'http://httpbin.org/get')
  <Response [200]>
相关问题