Python使用第三方包requests发送请求,实现接口自动化

发送请求分三步:

1、组装请求:包括请求地址、请求头header、cookies、请求数据等

2、发送请求,获取响应:支持get、post等方法

3、解析响应

一、最简单的get请求

requests.get(url=rul)

#coding=utf-8
import requests
url="http://www.baidu.com"
res=requests.get(url=url)
res.encoding='utf-8' #设置编码格式
print(res.text) #输出响应文本

二、带参数的get请求

request.get(url=url,params=params)

#coding=utf-8
import requests
url="http://www.baidu.com/s?wd=杭州"
res=requests.get(url=url)
res.encoding='utf-8'
print(res.text)

或者
#coding=utf-8
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
url="http://www.baidu.com/s"
params={"wd":"西湖"}
res=requests.get(url,params)
print(res.text)

三、传统表单类post请求

request.post(url=url,data=data)

#coding=utf-8
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
url="http://www.baidu.com/s"
data={"wd":"西湖"}
res=requests.post(url=url,data=data)
print(res.text)

四、json类型的post请求

requests.post(url=url,data=data)

#coding=utf-8
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
url="http://www.baidu.com/s"
data='''
{"wd":"西湖"}
'''
res=requests.post(url=url,data=data)
print(res.text)

Python使用requests发送请求的更多相关文章

  1. python使用requests发送application/x-www-form-urlencoded请求数据

    def client_post_formurlencodeddata_requests(request_url,requestJSONdata): #功能说明:发送以form表单数据格式(它要求数据名 ...

  2. python使用requests发送multipart/form-data请求数据

    def client_post_mutipart_formdata_requests(request_url,requestdict): #功能说明:发送以多部分表单数据格式(它要求post的消息体分 ...

  3. python使用requests发送application/json报文数据

    def client_post_jsondata_requests(request_url,requestJSONdata): #功能说明:发送json请求报文到指定的地址并获取请求响应报文 #输入参 ...

  4. python使用requests发送text/xml报文数据

    def client_post_xmldata_requests(request_url,requestxmldata): #功能说明:发送请求报文到指定的地址并获取请求响应报文 #输入参数说明:接收 ...

  5. 12.Python使用requests发送post请求

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  6. Python使用requests发送post请求的三种方式

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  7. Python爬虫requests判断请求超时并重新发送请求

     下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html  在爬虫的执行当中,总会遇到请求连接 ...

  8. Python - requests发送请求报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: 小明 is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

    背景 在做接口自动化的时候,Excel作为数据驱动,里面存了中文,通过第三方库读取中文当请求参数传入 requests.post() 里面,就会报错 UnicodeEncodeError: 'lati ...

  9. 利用python的requests发送http请求

    >>> from requests import put, get >>> put('http://localhost:5000/todo1', data={'da ...

随机推荐

  1. 动态JavaWeb工程的架构问题

    B/S 网络应用的分层 前端(表示层) 后端( 业务逻辑层, 数据库访问层 ) 分层的目的->分模块->解耦 1,表示层---和用户直接交互 html,js,css, servlet 2, ...

  2. Hive 报错 Failed to load class "org.slf4j.impl.StaticLoggerBinder".

    打开hive报错 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaultin ...

  3. python程序基础

    高级程序设计语言包括Python.C/C++.Java等 低级程序设计语言包括汇编语言和机器语言   Python是一种解释型语言,但为了提高运行效率,Python程序在 执行一次之后会自动生成扩展名 ...

  4. 精尽MyBatis源码分析 - MyBatis 的 SQL 执行过程(一)之 Executor

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  5. 牛客练习赛69 E 字串(哈希)

    题目链接 题目大意 给出一个长度为n排列p 规定一个区间 [l,r] (l<=r) 是 fair 的,当且仅当区间中最小值等于 l 并且最大值等于 r 求 fair 区间的个数 题目思路 我不会 ...

  6. go创建动态库

    *nix *nix创建so比较方便,写好go代码之后,直接一条命令搞定. go build -buildmode=c-shared -o libgobblob.so 命令执行之后,会生成libgobb ...

  7. Goland 2020.2.x 激活码永久破解教程 (最新Goland激活码!2020.11.26亲测可用!)

    在2020.11.26 Goland的用户们又迎来了一次更新,这就导致很多软件打开时候就提示Goland激活码已经失效,码小辫第一时间给各位分享了关于最新Goland激活破解教程! goland已经更 ...

  8. Golang性能分析与优化

    在公司的分享,去除了相关的敏感信息.

  9. AndroidStudio新导入项目,无法编译,rebuild、clean都无效

    此按钮,可以用gradle重新编译

  10. Fist—— 团队展示

    作业要求 软件工程1班 团队名称 Fist 这个作业的目标 团队合作开发项目,加强团队合作,进一步了解相应岗位. 作业正文 https://www.cnblogs.com/team4/p/137730 ...