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
上一篇:day16-requests的基本用法


下一篇:Python简单代码爬取网图:正则表达式re+requests库爬取静态网站图片,(需要正则表达式findall匹配图片地址)