requests与urllib 库

requests库git

  发送请求:github

    能够处理全部请求类型:get、post、put、Delete、Head、Options
      r = requests.get(''https://httpbin.org/')json

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

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

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

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

  传递参数code

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

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

   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()
相关文章
相关标签/搜索