资讯专栏INFORMATION COLUMN

python中Requests请求的安装方法与常见用法详解

89542767 / 257人阅读

  小编写这篇文章的一个主要目的,主要是给大家去做一个科普,解释关于python中的一些相关的用法实例,包括介绍了python Requests请求方法,还有一些具体用法的详细解释,具体内容,下面小编给大家详细的解答。


  一、requests


  request的说法网上有很多,简单来说就是就是python里的很强大的类库,可以帮助你发很多的网络请求,比如get,post,put,delete等等,这里最常见的应该就是get和post。


  二、requests安装方式


  $pip install requests
  $easy_install requests


  三、说说常见的两种请求,get和post


  1、get请求


  (1)参数直接跟在url后面,即url的“?”后面,以key=value&key=value的形式


  (2)由于get的参数是暴露在外面的,所以一般不传什么敏感信息,经常用于查询等操作


  (3)由于参数是跟在url后面的,所以上传的数据量不大


  2、post请求


  (1)参数可以写在url后面,也可以写在body里面


  (2)用body上传请求数据,上传的数据量比get大


  (3)由于写在body体里,相对安全


  post正文格式


  (1)form表单html提交数据的默认格式


  Content-Type:application/x-www-form-urlencoded


  例如:username=admin&password123


  (2)multipart-form-data.复合表单可转数据+文件


  (3)纯文本格式raw,最常见的json.xml html js


  Content-Type:application/json.text/xml.text/html


  (4)binary.二进制格式:只能上传一个文件


  四、requests发送请求


  1、requests发送get请求


  url="http://www.search:9001/search/"
  param={"key":"你好"}
  res=requests.get(url=url,params=params)


  2、request发送post请求(body是json格式,如果还带cookie)


  headers={'Content-Type':'application/json'}#必须有
  url="http://www.search:9001/search/"
  data={"key":"你好"}
  cookies={"uid":"1"}
  res=requests.post(url=url,headers=headers,data=data,cookies=cookies)


  3、request发送post请求(body是urlencoded格式)


  url="http://www.search:9001/search/"
  data={"key":"你好"}
  res=requests.post(url=url,headers=headers)


  4、request上传文件


  def post_file_request(url,file_path):
  if os.path.exists(file_path):
  if url not in[None,""]:
  if url.startswith("http")or url.startswith("https"):
  files={'file':open(file_path,'rb')}
  res=requests.post(url,files=files,data=data)
  return{"code":0,"res":res}
  else:
  return{"code":1,"res":"url格式不正确"}
  else:
  return{"code":1,"res":"url不能为空"}
  else:
  return{"code":1,"res":"文件路径不存在"}


  五、response


  request发送请求后,会返回一个response,response里有好多信息,我进行了一下封装,基本如下


  staticmethod
  def get_response_text(response):
  if response not in[None,""]:
  if isinstance(response,requests.models.Response):
  return{"code":0,"res":response.text.encode('utf-8').decode('unicode_escape')}#这种方式可以将url编码转成中文,返回响应文本
  else:
  return{"code":1,"res":"response不合法"}
  else:
  return{"code":1,"res":"response对像不能为空"}
  staticmethod
  def get_response_status_code(response):
  if response not in[None,""]:
  if isinstance(response,requests.models.Response):
  return{"code":0,"res":response.status_code}#返回响应状态吗
  else:
  return{"code":1,"res":"response不合法"}
  else:
  return{"code":1,"res":"response对像不能为空"}
  staticmethod
  def get_response_cookies(response):
  if response not in[None,""]:
  if isinstance(response,requests.models.Response):
  return{"code":0,"res":response.cookies}#返回cookies
  else:
  return{"code":1,"res":"response不合法"}
  else:
  return{"code":1,"res":"response对像不能为空"}
  staticmethod
  def get_response_headers(response):
  if response not in[None,""]:
  if isinstance(response,requests.models.Response):
  return{"code":0,"res":response.headers}#返回headers
  else:
  return{"code":1,"res":"response不合法"}
  else:
  return{"code":1,"res":"response对像不能为空"}
  staticmethod
  def get_response_encoding(response):
  if response not in[None,""]:
  if isinstance(response,requests.models.Response):
  return{"code":0,"res":response.encoding}#返回编码格式
  else:
  return{"code":1,"res":"response不合法"}
  else:
  return{"code":1,"res":"response对像不能为空"}
  补充:requests中遇到问题
  获取cookie
  #-*-coding:utf-8-*-
  #获取cookie
  import requests
  import json
  url="https://www.baidu.com/"
  r=requests.get(url)
  #将RequestsCookieJar转换成字典
  c=requests.utils.dict_from_cookiejar(r.cookies)
  print(r.cookies)
  print(c)
  for a in r.cookies:
  print(a.name,a.value)
  >>控制台输出:
  <RequestsCookieJar[<Cookie BDORZ=27315 for.baidu.com/>]>
  {'BDORZ':'27315'}
  BDORZ 27315
  发送Cookie
  #-*-coding:utf-8-*-
  #发送cookie到服务器
  import requests
  import json
  host="*****"
  endpoint="cookies"
  url=''.join([host,endpoint])
  #方法一:简单发送
  #cookies={"aaa":"bbb"}
  #r=requests.get(url,cookies=cookies)
  #print r.text


  #方法二:复杂发送


  s=requests.session()
  c=requests.cookies.RequestsCookieJar()
  c.set('c-name','c-value',path='/xxx/uuu',domain='.test.com')
  s.cookies.update(c)


  综上所述,这篇文章就给大家介绍到这里了,希望可以给大家带来更多的帮助。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128248.html

相关文章

  • Python requests 安装开发

    摘要:是用语言编写客户端库,跟类似,基于,但比更加方便,可以节约我们大量的工作,完全满足测试需求,编写爬虫和测试服务器响应数据时经常会用到。 Requests 是用Python语言编写HTTP客户端库,跟urllib、urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求,编写爬虫和测试服务器响应数据时经常会用到。Reque...

    XGBCCC 评论0 收藏0
  • Python requests 安装开发

    摘要:是用语言编写客户端库,跟类似,基于,但比更加方便,可以节约我们大量的工作,完全满足测试需求,编写爬虫和测试服务器响应数据时经常会用到。 Requests 是用Python语言编写HTTP客户端库,跟urllib、urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求,编写爬虫和测试服务器响应数据时经常会用到。Reque...

    zgbgx 评论0 收藏0
  • Python requests 安装开发

    摘要:是用语言编写客户端库,跟类似,基于,但比更加方便,可以节约我们大量的工作,完全满足测试需求,编写爬虫和测试服务器响应数据时经常会用到。 Requests 是用Python语言编写HTTP客户端库,跟urllib、urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求,编写爬虫和测试服务器响应数据时经常会用到。Reque...

    wuyangnju 评论0 收藏0
  • Python requests 安装开发

    摘要:是用语言编写客户端库,跟类似,基于,但比更加方便,可以节约我们大量的工作,完全满足测试需求,编写爬虫和测试服务器响应数据时经常会用到。 Requests 是用Python语言编写HTTP客户端库,跟urllib、urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求,编写爬虫和测试服务器响应数据时经常会用到。Reque...

    2shou 评论0 收藏0
  • Python requests 安装开发

    摘要:是用语言编写客户端库,跟类似,基于,但比更加方便,可以节约我们大量的工作,完全满足测试需求,编写爬虫和测试服务器响应数据时经常会用到。 Requests 是用Python语言编写HTTP客户端库,跟urllib、urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求,编写爬虫和测试服务器响应数据时经常会用到。Reque...

    happyfish 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<