资讯专栏INFORMATION COLUMN

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

89542767 / 554人阅读

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


  一、requests


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


  二、requests安装方式


</>复制代码

  1.   $pip install requests
  2.   $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请求


</>复制代码

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


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


</>复制代码

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


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


</>复制代码

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


  4、request上传文件


</>复制代码

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


  五、response


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


</>复制代码

  1.   staticmethod
  2.   def get_response_text(response):
  3.   if response not in[None,""]:
  4.   if isinstance(response,requests.models.Response):
  5.   return{"code":0,"res":response.text.encode('utf-8').decode('unicode_escape')}#这种方式可以将url编码转成中文,返回响应文本
  6.   else:
  7.   return{"code":1,"res":"response不合法"}
  8.   else:
  9.   return{"code":1,"res":"response对像不能为空"}
  10.   staticmethod
  11.   def get_response_status_code(response):
  12.   if response not in[None,""]:
  13.   if isinstance(response,requests.models.Response):
  14.   return{"code":0,"res":response.status_code}#返回响应状态吗
  15.   else:
  16.   return{"code":1,"res":"response不合法"}
  17.   else:
  18.   return{"code":1,"res":"response对像不能为空"}
  19.   staticmethod
  20.   def get_response_cookies(response):
  21.   if response not in[None,""]:
  22.   if isinstance(response,requests.models.Response):
  23.   return{"code":0,"res":response.cookies}#返回cookies
  24.   else:
  25.   return{"code":1,"res":"response不合法"}
  26.   else:
  27.   return{"code":1,"res":"response对像不能为空"}
  28.   staticmethod
  29.   def get_response_headers(response):
  30.   if response not in[None,""]:
  31.   if isinstance(response,requests.models.Response):
  32.   return{"code":0,"res":response.headers}#返回headers
  33.   else:
  34.   return{"code":1,"res":"response不合法"}
  35.   else:
  36.   return{"code":1,"res":"response对像不能为空"}
  37.   staticmethod
  38.   def get_response_encoding(response):
  39.   if response not in[None,""]:
  40.   if isinstance(response,requests.models.Response):
  41.   return{"code":0,"res":response.encoding}#返回编码格式
  42.   else:
  43.   return{"code":1,"res":"response不合法"}
  44.   else:
  45.   return{"code":1,"res":"response对像不能为空"}
  46.   补充:requests中遇到问题
  47.   获取cookie
  48.   #-*-coding:utf-8-*-
  49.   #获取cookie
  50.   import requests
  51.   import json
  52.   url="https://www.baidu.com/"
  53.   r=requests.get(url)
  54.   #将RequestsCookieJar转换成字典
  55.   c=requests.utils.dict_from_cookiejar(r.cookies)
  56.   print(r.cookies)
  57.   print(c)
  58.   for a in r.cookies:
  59.   print(a.name,a.value)
  60.   &gt;&gt;控制台输出:
  61.   &lt;RequestsCookieJar[&lt;Cookie BDORZ=27315 for.baidu.com/&gt;]&gt;
  62.   {'BDORZ':'27315'}
  63.   BDORZ 27315
  64.   发送Cookie
  65.   #-*-coding:utf-8-*-
  66.   #发送cookie到服务器
  67.   import requests
  68.   import json
  69.   host="*****"
  70.   endpoint="cookies"
  71.   url=''.join([host,endpoint])
  72.   #方法一:简单发送
  73.   #cookies={"aaa":"bbb"}
  74.   #r=requests.get(url,cookies=cookies)
  75.   #print r.text


  #方法二:复杂发送


</>复制代码

  1.   s=requests.session()
  2.   c=requests.cookies.RequestsCookieJar()
  3.   c.set('c-name','c-value',path='/xxx/uuu',domain='.test.com')
  4.   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...

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

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

    2shou 评论0 收藏0

发表评论

0条评论

89542767

|高级讲师

TA的文章

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