zoukankan      html  css  js  c++  java
  • Requests库入门

    安装:

    $ pip install requests

    Response对象的一些基本属性:

      Response.status_code 请求的返回状态,正常为200

      Response.text 页面的字符串形式

      Response.encoding header中定义的编码(若header中未定义则会出问题)

      Response.apparent_encoding 根据页面分析得到的编码

      Response.content http相应内容的二进制形式

      Response.raise_for_status() 若statuscode不为200抛出一个异常(HTTPError)

    Requests库的异常:

      requests.ConnectionError:网络连接错误异常,如查询dns失败,拒绝连接等.

      requests.HTTPError

      requests.URLRequired

      requests.TooManyRedirects:超过最大重定向次数

      requests.ConnectTimeout:连接远程服务器超时异常

      requests.Timeout:请求URL超时,产生超时异常

     

     

     **kwargs:

    params:字典或者字节序列,作为参数增加到url里

      如:

      kv={'key1':'value1','key2':'value2'}

      r = reqeusts.reqeusts('GET',url,params=kv)

    data:字典,字节序列或者文件对象,作为request的内容

      r = reqeusts.request('POST',url,data = kv)

      r = reqursts.request('POST',url,data=string)

    json:

    headers:对应http头

      可修改http协议头

      hd = {"user-agent":"Chrome/10"}

      r = requests.request('GET',url,headers = hd)

    cookies:字典或者cookieJar,requests中的cookie

    auth:

    files:

    timeout:以秒为单位,超时将产生timeout异常

    proxies:字典类型,设定访问代理服务器,可以增加登录认证

    allow_redirects:True/False,默认为True,重定向开关

    stream:True/False,默认为True,获取内容是否立即下载

    verify:True/False,默认为True,验证SSL证书

    cert:本地ssl证书路径

  • 相关阅读:
    延迟加载和缓存
    动态SQL
    Mybatis框架模糊查询+多条件查询
    mybatis增删改
    初始mybatis(二)
    Struts2框架和SpringMvc框架的区别
    MyBatis框架与Hibernate 框架的区别
    初始mybatis
    Servlet
    find命令使用
  • 原文地址:https://www.cnblogs.com/Alexzzzz/p/7609197.html
Copyright © 2011-2022 走看看