• requests模块


    方法 

    # r = requests.post('http://httpbin.org/post')
    # r = requests.put('http://httpbin.org/put')
    # r = requests.delete('http://httpbin.org/delete')
    # r = requests.head('http://httpbin.org/get')
    # r = requests.options('http://httpbin.org/get')
    ...
    
    requests.request(method='post', ...)

    参数

    2.1  url
    2.2  headers
        headers = {
            'cookie': '_xsrf=qgpErUtmbbBx4iIjaEvCvrgS1LTV7gzc; _zap=cf42d0d5-b056-4e0d-b3a3-26dfd2f6a374; d_c0="AODm6prxAQ6PTp0ckYQVU1Clh8qZB3pTJ_k=|1533428612"; l_n_c=1; n_c=1; __gads=ID=5cd6f7750f3e001f:T=1543593535:S=ALNI_MbSXYYBZxCv58del3tLMtSWBWAOjQ; q_c1=24324f549ec242e5aa8bbd486b11f2f9|1553787786000|1533428614000; __utma=51854390.2001133214.1553787789.1553787789.1553787789.1; __utmc=51854390; __utmz=51854390.1553787789.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __utmv=51854390.000--|3=entry_date=20180805=1; l_cap_id="MGVkZWYxYTdjMjBlNGZiNjliN2NkY2E4NDA1NmY2Yjk=|1553828944|74f826e94ebd276887ae1109645c490cfc0024e1"; r_cap_id="Y2NjNzc3YmZhNGIxNDFlMThjYjljODAyYTAxZjUxNGI=|1553828944|e62fe661882dccbb3ab763c8ba1a78af5c2cbf1e"; cap_id="MDNhOTJiOGQzNjMxNGI3OWEyMjZhMzQ1MDk1NGIxMTc=|1553828944|09c7b9ef0ad1cc49d5becb57988a720a8afacaea"; tgw_l7_route=80f350dcd7c650b07bd7b485fcab5bf7; capsion_ticket="2|1:0|10:1554214536|14:capsion_ticket|44:YmYwMjRlNWU0NzQ2NDNmNDk2Y2Y5ZTU3NzM5YTM4YjM=|0d9264d4b65c02936905137db26dd54974d02e9f074893cf4a4ab26370eb9383"; z_c0="2|1:0|10:1554214562|4:z_c0|92:Mi4xV1ZGNEFnQUFBQUFBNE9icW12RUJEaVlBQUFCZ0FsVk5vcnlRWFFEWWJlaFU2QzZEY3BEa1RTejFxQlcxSlpubmpB|947346f0ac55e10714da8a6f6d9753534a4f5a22aaf6ab1d69e734c8549c95bf"; unlock_ticket="ABBKw-YyTQkmAAAAYAJVTap1o1y90hDD4p9aVPDBfFiiXoRFNKP_LA=="; tst=r',
            'host': 'www.zhihu.com',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
        }
        r = requests.get('https://www.zhihu.com/people/sun-chang-heng/activities', headers=headers)     # 我的主页
        
    2.3  cookies
        ret = requests.get(uri)  # 微信返回的凭证
        session['cookie1'] = ret.cookies.get_dict() 
        rep = requests.get(url=user_url, cookies=session['cookie1'])
    2.4  params
    2.5  data,传请求体
            
            requests.post(
                ...,
                data={'user':'alex','pwd':'123'}
            )
            
            GET /index http1.1
    host:c1.com
    
    user=alex&pwd=123
            
    2.6  json,传请求体
            requests.post(
                ...,
                json={'user':'alex','pwd':'123'}
            )
            
            GET /index http1.1
    host:c1.com
    Content-Type:application/json
    
    {"user":"alex","pwd":123}
    2.7 代理 proxies
        # 无验证
            proxie_dict = {
                "http": "61.172.249.96:80",
                "https": "http://61.185.219.126:3128",
            }
            ret = requests.get("https://www.proxy360.cn/Proxy", proxies=proxie_dict)
            
        
        # 验证代理
            from requests.auth import HTTPProxyAuth
            
            proxyDict = {
                'http': '77.75.105.165',
                'https': '77.75.106.165'
            }
            auth = HTTPProxyAuth('用户名', '密码')
            
            r = requests.get("http://www.google.com",data={'xxx':'ffff'} proxies=proxyDict, auth=auth)
            print(r.text)
    -----------------------------------------------------------------------------------------
    2.8 文件上传 files
        # 发送文件
            file_dict = {
                'f1': open('xxxx.log', 'rb')
            }
            requests.request(
                method='POST',
                url='http://127.0.0.1:8000/test/',
                files=file_dict
            )
            
    2.9 认证 auth
    
        内部:
            用户名和密码,用户和密码加密,放在请求头中传给后台。
            
                - "用户:密码"
                - base64("用户:密码")
                - "Basic base64("用户|密码")"
                - 请求头:
                    Authorization: "basic base64("用户|密码")"
            
        from requests.auth import HTTPBasicAuth, HTTPDigestAuth
    
        ret = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('wupeiqi', 'sdfasdfasdf'))
        print(ret.text)
        
    2.10 超时 timeout 
        # ret = requests.get('http://google.com/', timeout=1)
        # print(ret)
    
        # ret = requests.get('http://google.com/', timeout=(5, 1))
        # print(ret)
        
    2.11 允许重定向  allow_redirects
        ret = requests.get('http://127.0.0.1:8000/test/', allow_redirects=False)
        print(ret.text)
        
    2.12 大文件下载 stream
        from contextlib import closing
        with closing(requests.get('http://httpbin.org/get', stream=True)) as r1:
        # 在此处理响应。
        for i in r1.iter_content():
            print(i)
            
    2.13 证书 cert
        - 百度、腾讯 => 不用携带证书(系统帮你做了)
        - 自定义证书
            requests.get('http://127.0.0.1:8000/test/', cert="xxxx/xxx/xxx.pem")
            requests.get('http://127.0.0.1:8000/test/', cert=("xxxx/xxx/xxx.pem","xxx.xxx.xx.key"))
    2.14 确认 verify = False 

    响应

    r = requests.get('http://www.baidu.com')
    print(type(r.status_code), r.status_code)   # 状态码
    print(type(r.headers), r.headers)           # 响应头
    print(type(r.cookies), r.cookies)           # cookie
    print(type(r.cookies), r.cookies.get_dict())           # 字典格式的cookie
    print(type(r.url), r.url)                   # url
    print(type(r.history), r.history)           #
    r.encoding = 'utf-8'    # 设置编码
    print(type(r.text), r.text)                 # 响应体内容
    print('Request Successfully') if r.status_code == requests.codes.ok else print('返回失败')
  • 相关阅读:
    vs13的内存占用 关闭之
    Java基础 -5
    Java基础 -4.6
    Java基础 -4.5
    Java基础 -4.4
    Java基础 -4.3
    Java基础 -4.2
    Java基础 -4
    Java基础 -3.5
    Java基础 -3.4
  • 原文地址:https://www.cnblogs.com/sunch/p/10764041.html
Copyright © 2020-2023  润新知