【文章末尾给大家留下大量的福利喔】
1,导入requests模块
==============================================================
get请求:
url = host = 'https://www.baidu.com/s'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'}
data = {'wd':'猫'}
response = requests.get(url=url,headers=headers,params=data,json=json,data=data)
print(response.content.decode("utf-8"))
get请求中的参数可以在URL中,也可以在字典中
===========================================================================
响应的解码方法有几种方式:
第一种方法:
response.encoding = "utf-8" # 类型:str 解码:自动根据响应头进行推测
print(response.text) # 直接text打印是乱码,因为text默认返回的是Unicode格式
第二种方法:
print(response.content.decode("utf-8")) # content是二进制模式,通过指定utf-8格式输出
第三种方法:
response.encoding = response.apparent_encoding # 解码:系统自动推测 如果不知道是什么编码格式,就使用这个方法
print(response.text)
获取基本信息:
print("状态码:",response.status_code)
print("请求URL:",response.url)
print("头部信息:",response.headers)
print("cookie信息:",response.cookies)
==========================================================================
post请求:
tag_url = 'https://api.weixin.qq.com/cgi-bin/tags/create'
tag_data = {'access_token':token}
tag_json_body = {"tag":{"name" :"长沙002"} }
headers = {"content-type":"application/json"} # 发送json 数据必须带有头部信息 content-type
response = requests.post(url=tag_url,params=tag_data,headers=headers,data=json.dumps(tag_json_body))
print(response.content.decode("utf-8"))
发送post请求的基础知识 dumps和loads
str1 = json.dumps(str_dict) # json.dumps 是把字典、json对象转成字符串
str_json = json.loads(str2) # json.loads 是把字符串转成json对象
注:response一般返回的数据都为字符串格式 /
也可通过json_obj=response.json()的方式转成json格式,可以通过json_obj["key"]提取json中key的值
# 发送json 数据必须带有头部信息 content-type :headers = {"content-type":"application/json"}
=====================================================================
自动化测试相关教程推荐:
2023最新自动化测试自学教程新手小白26天入门最详细教程,目前已有300多人通过学习这套教程入职大厂!!_哔哩哔哩_bilibili
2023最新合集Python自动化测试开发框架【全栈/实战/教程】合集精华,学完年薪40W+_哔哩哔哩_bilibili
测试开发相关教程推荐
2023全网最牛,字节测试开发大佬现场教学,从零开始教你成为年薪百万的测试开发工程师_哔哩哔哩_bilibili
postman/jmeter/fiddler测试工具类教程推荐
讲的最详细JMeter接口测试/接口自动化测试项目实战合集教程,学jmeter接口测试一套教程就够了!!_哔哩哔哩_bilibili
2023自学fiddler抓包,请一定要看完【如何1天学会fiddler抓包】的全网最详细视频教程!!_哔哩哔哩_bilibili
2023全网封神,B站讲的最详细的Postman接口测试实战教学,小白都能学会_哔哩哔哩_bilibili
2,导入json模块:
通过json把数据转成字符串或者json对象
1,json.dumps 是把字典、json对象转换为字符串
2,json.loads 是将字符串转成字典,json对象
=====================================================================
3,上传文件:
wx_url = "https://api.weixin.qq.com/cgi-bin/media/upload"
wx_data = {"access_token":token,"type":"image"}
file = {"files":open("E:/12345678.png","rb")} # 注意:必须要用字典的方式open
res = requests.post(url=wx_url,params=wx_data,files=file)
print(res.content.decode("utf-8"))
========================================================================
4,设置代理
# 设置代理,多用于爬虫
proxies = {"http":"http://12.34.56.79:9527",
"https":"https://12.34.56.79:9527"}
# 1,普通的代理
# res = requests.get(url="http://www.hnxmxit.com",proxies=proxies)
# print(res.content.decode("utf-8"))
# 2,携带了登录的用户名和密码
proxies = {"http":"http://用户名:密码@12.34.56.79:9527"}
res = requests.get(url="http://hmxmxit.com",proxies=proxies)
print(res.content.decode("utf-8"))
========================================================================
5,连接超时
# 2,链接超时,接收超时
print(time.time())
res = requests.get(url="http://www.hnxmxit.com",timeout=(0.01,2)) # 以秒为单位(连接,接收)
print(time.time())
==========================================================================
6,重定向
res = requests.get(url="http://www.360buy.com",allow_redirects=False) # 设置重定向开关
print(res.history) # 查看重定向历史
print(res.url)
==========================================================================
7,添加cookie
1,可以在参数中添加cookie
2,在header中添加cookie
# 1,在参数中添加
# cookie_dict = {"login_name":"admin"}
# res = requests.get(url="http://www.hnxmxit.com",cookies=cookie_dict)
# 2,在头部添加
cookie_header = {"login_name":"root"}
res = requests.get(url="http://www.hnxmxit.com",headers=cookie_header)
==========================================================================
8,关闭证书:
# 关闭警告
# requests.packages.urllib3.disable_warnings()
# warnings.filterwarnings("ignore")
# 1,关闭证书
# res = requests.get(url="https://www.12306.cn",verify=False)
# print(res.content.decode("utf-8"))
# 2,安装pyopenssl模块 可以验证证书,可以在请求中不加 verify=False
res = requests.get(url="https://www.12306.cn")
print(res.content.decode("utf-8"))
# 3,加上证书,找公司开发人员要 .crt文件 推荐这种
res = requests.get(url="https://www.12306.cn",cert=("/路径/证书.crt","/路径/key"))
==============================================================================
9,请求异常处理
ReadTimeout (加载超时)
ConnectionError(链接超时)
RequestException (request错误)
导入包
import requests
from requests.exceptions import ReadTimeout,ConnectionError,RequestException
try:
res = requests.get(url="http://www.hnxmxit.com",timeout=(0.01,1))
print(res.status_code)
except ReadTimeout as e:
print("加载超时")
print(e)
except ConnectionError:
print("链接超时")
except RequestException as e:
print(e)
=============================================================================
10,re模块
# 写法1:用re的函数进行匹配,会取第一个
str1 = "python3helloworldpython4"
value = re.search("python\d",str1).group()
print(value)
# 写法2:用正则表达式对象
reg_obj = re.compile("python\d") # 创建正则对象
value2 = reg_obj.search(str1).group()
print(value2)
title_str = "<title>000</title>"
reg_obj2 = re.compile("t+")
value3 = reg_obj2.findall(title_str)[0] # findall 返回的是一个列表
print(value3)
value4 = re.findall("<title>(.+?)</title>",title_str)[0]
print(value4)
=======================================================================
=======================================================================
11,设置 session 会话保持连接
session_obj=requests.session() # 用来保持会话连接,后面的请求都用session对象来发送
# session添加cookie
# 方法2: 推荐使用
# session_obj.cookies.set("test_add_cookie","newdream")
# response = session_obj.get(url="http://www.hnxmxit.com")
13,添加cookie绕过登录phpwind论坛
# 1.手动打开论坛获取登录后的cookie (在F12下找Application的cookies)
# 2.添加cookie
session_obj = requests.session()
session_obj.cookies.set("zFb_lastvisit","142%091621076949",domain="47.107.178.45",path="/")
session_obj.cookies.set("zFb_winduser","WNirQHIeouFINxWV",domain="47.107.178.45",path="/")
=================================================================================
12,封装get/post请求,封装测试类
class run_test:
def __init__(self)
def send_get()
def send_post()
总结:
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
如果对你有帮助的话,点个赞收个藏,给作者一个鼓励。也方便你下次能够快速查找。
如有不懂还要咨询下方小卡片,博主也希望和志同道合的测试人员一起学习进步
在适当的年龄,选择适当的岗位,尽量去发挥好自己的优势。
我的自动化测试开发之路,一路走来都离不每个阶段的计划,因为自己喜欢规划和总结,
测试开发视频教程、学习笔记领取传送门!!