requests与urllib 库

requests库

  发送请求:

    可以处理所有请求类型:get、post、put、Delete、Head、Options
      r = requests.get(''https://httpbin.org/')

      r = requests.post('https://httpbin.org/post')

      r = requests.put('https://httpbin.org/put')

      r = requests.delete('https://httpbin.org/delete')

      r = requests.options('https://httpbin.org/options')

  传递参数

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

   r = requests.get("http://httpbin.org/get",params=payload)

   print(r.url)

 

   相应内容

    requests 读取相应内容

    r = requests.get('https://github.com/timeline.json')

     r.text

    

   推测文本编码,找出Requests使用了什么编码 并使用r.encoding属性改变

    r.encoding = 'ISO-8859-1'



  JSON相应内容
  Requests中也内置JSON解码器
  
  r = requests.get('https://github.com/timeline.json')
  
r.json()
  相应状态码
  
r = requests.get('http://httpbin.org/get')
  
r.status_code == requests.codes.ok
  抛出异常处理
  
bad_r = requests.get('http://httpbin.org/status/404')
  
bad_r.raise_for_status()
 
 

  


    

      

posted @ 2018-02-01 19:21  探出的头  阅读(167)  评论(0编辑  收藏  举报