requests库底层代码实现

import requests

class RequestSpider(object):
    def __init__(self):
        url = "http://www.baidu.com"

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

        #发送网络请求
        self.response = requests.get(url, headers=headers)

    def run(self):

        data = self.response.content
        print(data)
        #1.获取请求头
        requests_header = self.response.request.headers
        print(requests_header)

        #2.获取响应头
        response_header = self.response.headers
        print(response_header)

        #3.响应的状态码
        code = self.response.status_code
        print(code)
        #4.请求的cookie(有些请求是不需要cookie)
        requests_cookie = self.response.request._cookies
        print(requests_cookie)
        #浏览器自带cookie
        #5.响应cookie
        response_cookies = self.response.cookies
        print(response_cookies)

RequestSpider().run()

你可能感兴趣的:(requests库底层代码实现)