码迷,mamicode.com
首页 > 其他好文 > 详细

Requests

时间:2017-11-05 10:51:46      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:获取   option   let   jar   params   code   请求   text   options   


1、Requests 用法

  例子:

  import requests

  response = requests.get(‘https://www.baidu.com‘)

  print(type(response))

  print(response.status_code)  //200

  print(type(response.text))  

  print(response.text)

  print(response.cookies)  //不需要声明cookiejar

  各种请求方式

  import requests

  requests.post(‘http://httpbin.org/post‘)

  requests.put(‘http://httpbin.org/post‘)

  requests.delete(‘http://httpbin.org/post‘)

  requests.options(‘http://httpbin.org/post‘)

  基本Get请求

  import requests

  response =requests.get(‘http://httpbin.org/get‘)

  print(response.text)

 

  带参数GET请求
  import requests

  response =requests.get(‘http://httpbin.org/get?name=germey&age=22‘)

  print(response.text)

  或者

  data={

    ‘name‘:‘germey‘,

    ‘age‘:22

  }

  response =requests.get(‘http://httpbin.org/get‘,params=data)

  print(response.text)

 

  解析Json 方法

  import requests

  response =requests.get(‘http://httpbin.org/get‘)

  print(type(response.text))

  print(response.json())  //json

  或

  print(json.loads(response.text))

  print(type(response.json()))

 

  获取二进制数据

  import requests

  response=requests.get(‘https://github.com/favicon.ico‘)

  print(type(response.text),type(response.content))

  print(response.text)

  print(response.content)

  保存图片

  import requests

  response=requests.get(‘https://github.com/favicon.ico‘)

  with open(‘favicon.icon‘,‘wb‘) as f:

    f.write(response.content)

    f.close()

  

  添加headers

  import  requests

  response= requests.get(‘https://www.zhihu.com/expiore‘)

  print(response.text)

 

Requests

标签:获取   option   let   jar   params   code   请求   text   options   

原文地址:http://www.cnblogs.com/youmingkuang/p/7786778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!