requests Python笔记(一)

2021/10/31 12:39:51

本文主要是介绍requests Python笔记(一),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

我准备做一个web工具集合
在:

class resG(requests.get):

遭遇报错
TypeError: function() argument 'code' must be code, not str
查询了一下,原因是父类不是一个类,又找了一下requests的源代码
requests.__init__的import有这么一段

from .api import request, get, head, post, patch, put, delete, options

于是去api.py里查找
发现了requests.api.get

def get(url, params=None, **kwargs):
    return request('get', url, params=params, **kwargs)

requests.api.requests

def request(method, url, **kwargs):
    with sessions.Session() as session:
        return session.request(method=method, url=url, **kwargs)

查找sessions

from . import sessions

找sessions.py中的Session

    def __init__(self):
        self.headers = default_headers()
        self.auth = None
        self.proxies = {}
        self.hooks = default_hooks()
        self.params = {}
        self.stream = False
        self.verify = True
        self.cert = None
        self.max_redirects = DEFAULT_REDIRECT_LIMIT
        self.trust_env = True
        self.cookies = cookiejar_from_dict({})
        self.adapters = OrderedDict()
        self.mount('https://', HTTPAdapter())
        self.mount('http://', HTTPAdapter())

requests.sessions.Session.requests

def request(self, method, url,
            params=None, data=None, headers=None, cookies=None, files=None,
            auth=None, timeout=None, allow_redirects=True, proxies=None,
            hooks=None, stream=None, verify=None, cert=None, json=None):=
        req = Request(
            method=method.upper(),
            url=url,
            headers=headers,
            files=files,
            data=data or {},
            json=json,
            params=params or {},
            auth=auth,
            cookies=cookies,
            hooks=hooks,
        )
        prep = self.prepare_request(req)

        proxies = proxies or {}

        settings = self.merge_environment_settings(
            prep.url, proxies, stream, verify, cert
        )

        # Send the request.
        send_kwargs = {
            'timeout': timeout,
            'allow_redirects': allow_redirects,
        }
        send_kwargs.update(settings)
        resp = self.send(prep, **send_kwargs)

        return resp


这篇关于requests Python笔记(一)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程