zoukankan      html  css  js  c++  java
  • Requests文档 阅读笔记

    Request库

    Requests is an elegant and simple HTTP library for Python, built for human beings.

    Request库是日常中经常使用的库,总结一下吧。附上文档Requests: 让 HTTP 服务人类

    发送请求

    #使用requests库前都要导入requests库
    import requests     
    
    #发送GET,POST,PUT,DELETE,HEAD 以及 OPTIONS 请求
    r = requests.get('https://www.cnblogs.com/thenbz3/')
    r = requests.post('https://www.cnblogs.com/thenbz3/')
    r = requests.put('https://www.cnblogs.com/thenbz3/')
    r = requests.delete('https://www.cnblogs.com/thenbz3/')
    r = requests.head('https://www.cnblogs.com/thenbz3/')
    r = requests.options('https://www.cnblogs.com/thenbz3/')
    

    传递URL参数

    #GET请求时传递参数
    payload = {'key1':'value1', 'key2':'value2', 'key3' : ['value3','valua4']}
    r = requests.get('http://httpbin.org/get', params = payload)
    print(r.url)
    
    #相当于
    r = requests.get('http://httpbin.org/get?key1=value1&key2=value2&key3=value3&key3=value4')
    

    更加复杂的POST请求

    #POST请求时传递参数
    import requests
    
    payload = {'key1' : 'value1', 'key2' : 'value2'}
    r = requests.post('http://httpbin.org/post', data = payload)
    
    #或者用元组列表
    paylaod = (('key1', 'value1'),('key1', 'value2'))
    r = requests.post('http://httpbin.org/post', data = payload)
    

    响应内容

    #以GET请求举例,会以html代码形式返回页面的内容
    import requests 
    
    url = 'https://www.cnblogs.com/thenbz3/'
    r = request.get(url)
    html = t.text
    print(html)
    print(r.encoding)
    
    #还可以更改返回页面内容的编码
    r.encoding = 'utf-8' 
    print(r.encoding)
    
    
    # 响应状态码
    r.status_code
    r.status_code == requests.codes.ok   #内置状态码查询对象
    r.raise_for_status()                 #抛出异常,无异常返回None
    print(r)
    
    

    定制请求头

    import requests
    
    url = 'https://www.cnblogs.com/thenbz3/'
    headers = {'user-agent' : 'my-app/o.o1'}
    r=request.get(url,headers = headers)
    
    r.cookies['cookie的键名']
    
    #发送请求时带cookie
    url = 'http://httpbin.org/cookies'
    cookies = dict(cookies_are='working')
    r = requests.get(url, cookies=cookies)
    
    #Cookie Jar
    jar = requests.cookies.RequestsCookieJar()
    jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
    jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
    url = 'http://httpbin.org/cookies'
    r = requests.get(url, cookies=jar)
    r.text
    
    '{"cookies": {"tasty_cookie": "yum"}}'
    

    重定向

    #allow_redirects=False  禁用重定向
    r = requests.get('http://github.com', allow_redirects=False)
    
    

    会话对象Session

    #会保持同一个cookie,和某些参数,在编写脚本的时候如果没有特殊要求,最好用session
    import requests
    
    s = requests.session()
    
    s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
    r = s.get("http://httpbin.org/cookies")
    
    print(r.text)
    # '{"cookies": {"sessioncookie": "123456789"}}'
    
    
    #不过需要注意,就算使用了会话,方法级别的参数也不会被跨请求保持。下面的例子只会和第一个请求发送 cookie ,而非第二个
    r = s.get('http://httpbin.org/cookies', cookies={'from-my': 'browser'})
    print(r.text)
    # '{"cookies": {"from-my": "browser"}}'
    
    r = s.get('http://httpbin.org/cookies')
    print(r.text)
    # '{"cookies": {}}'
    
    
  • 相关阅读:
    C++ <cstring> 里的一些常用函数
    Hadoop_第一次作业
    线性回归理解和应用例子
    条款28 :避免返回handles指向对象内部成分
    条款25 :尽可能延后变量定义式的出现时间
    条款21 :必须返回对象时,别妄想返回其reference
    条款16:成对使用new和delete时要采用相同的形式
    条款22 :将成员变量声明为private
    条款13:以对象管理资源
    条款12:复制对象时勿忘其每一个成分
  • 原文地址:https://www.cnblogs.com/thenbz3/p/11453497.html
Copyright © 2011-2022 走看看