python请求方式重封装

可用于请求头切换,模拟多种浏览器

import requests

# 当前工具包是封装了接口的请求方式

class Proxy(object):

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
    }

    @classmethod
    def get(cls,url, params=None,headers=None,**kwargs):
        r"""Sends a GET request.

        :param url: URL for the new :class:`Request` object.
        :param params: (optional) Dictionary, list of tuples or bytes to send
            in the query string for the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :return: :class:`Response ` object
        :rtype: requests.Response
        """
        kwargs.setdefault('allow_redirects', True)
        kwargs.update({"headers":cls.headers})
        return requests.request('get', url, params=params ,**kwargs)

    @classmethod
    def options(cls,url, **kwargs):
        r"""Sends an OPTIONS request.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :return: :class:`Response ` object
        :rtype: requests.Response
        """

        kwargs.setdefault('allow_redirects', True)
        kwargs.update({"headers": cls.headers})
        return requests.request('options', url, **kwargs)

    @classmethod
    def head(cls,url, **kwargs):
        r"""Sends a HEAD request.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes. If
            `allow_redirects` is not provided, it will be set to `False` (as
            opposed to the default :meth:`request` behavior).
        :return: :class:`Response ` object
        :rtype: requests.Response
        """

        kwargs.setdefault('allow_redirects', False)
        kwargs.update({"headers": cls.headers})
        return requests.request('head', url ,**kwargs)

    @classmethod
    def post(cls,url, data=None, json=None, **kwargs):
        r"""Sends a POST request.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, 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 \*\*kwargs: Optional arguments that ``request`` takes.
        :return: :class:`Response ` object
        :rtype: requests.Response
        """
        kwargs.update({"headers": cls.headers})
        return requests.request('post', url, data=data, json=json, **kwargs)

    @classmethod
    def put(cls,url, data=None, **kwargs):
        r"""Sends a PUT request.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, 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 \*\*kwargs: Optional arguments that ``request`` takes.
        :return: :class:`Response ` object
        :rtype: requests.Response
        """
        kwargs.update({"headers": cls.headers})
        return requests.request('put', url, data=data, **kwargs)

    @classmethod
    def patch(cls,url, data=None, **kwargs):
        r"""Sends a PATCH request.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, 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 \*\*kwargs: Optional arguments that ``request`` takes.
        :return: :class:`Response ` object
        :rtype: requests.Response
        """
        kwargs.update({"headers": cls.headers})
        return requests.request('patch', url, data=data, **kwargs)

    @classmethod
    def delete(cls,url, **kwargs):
        r"""Sends a DELETE request.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        :return: :class:`Response ` object
        :rtype: requests.Response
        """
        kwargs.update({"headers": cls.headers})
        return requests.request('delete', url, **kwargs)

你可能感兴趣的:(python,开发语言)