Bootstrap

Python+Requests模拟发送GET请求

模拟发送GET请求

前置条件:导入requests库

一、发送不带参数的get请求

代码如下:

以百度首页为例

import requests

# 发送get请求
response = requests.get(url="http://www.baidu.com")
print(response.content.decode("utf-8"))  # 以utf-8的编码输出内容

二、发送带参数的get请求

发送带参数的get请求有几种方式

方式一:参数在URL中

代码如下:

以百度首页为例

import requests

# 发送带参数的get请求
# 方式一:参数在URL中
# http 协议,www.baidu.com 主机号,/s 请求地址,wd=猫 参数
host = "http://www.baidu.com/s?wd=猫"
# 因为百度服务器会对头部信息做检查所以需要添加请求头
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}
response = requests.get(url=host,headers=headers)
print(response.content.decode("utf-8"))

方式二:参数在字典中

代码如下:

以百度首页为例

import requests

# 方式二:参数在字典中
host = "http://www.baidu.com/s"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}
data = {'wd':'金钱豹'}  # 参数在字典中
response = requests.get(url=host,headers=headers,params=data)
print(response.content.decode('utf-8'))

获取响应数据的基本信息

代码如下:

以百度首页为例

import requests

# 发送get请求
response = requests.get(url="http://www.baidu.com")
# 获取响应的基本信息
print( "状态码:", response.status_code )
print( "请求URL:",response.url )
print( "头部信息:", response.headers )
print( "cookie信息:", response.cookies )
print("字节形式信息:",response.content )
print("文本信息:",response.text)
print("编码格式:",response.encoding)

封装get请求

代码示例:

# 封装get请求
def send_get(url):
    response = requests.get(url=url)
    return response

print(send_get("https://www.jd.com/").content.decode("utf-8"))

封装main方法

代码示例:

# 封装main方法
def run_main(method,url,data=None,json_info=None):
    response = None
    if method == "GET":
        response = send_get(url,data)
    elif method == "POST":
        response = send_post(url,data,json_info)
    else:
        print("参数错误")
        response = None
    return response

print(run_main("GET","https://www.jd.com/").content.decode("utf-8"))

封装测试类

代码示例:

# 将写好的get、post、run_mian方法做成类
import requests
class run_test:
    session_obj = requests.session()
    def __init__(self,method,url,params=None,data=None,headers=None):
        self.method = method
        self.url = url
        self.params = params
        self.data = data
        self.headers = headers

    def send_get(self):
        res = run_test.session_obj.get(url=self.url,params=self.params,headers=self.headers)
        return res
    def send_post(self):
        res = run_test.session_obj.post(url=self.url,params=self.params,
                                        data=self.data,headers=self.headers)
        return res

    def run_main(self):
        if self.method == "GET":
            res = self.send_get()
        elif self.method == "POST":
            res = self.send_post()
        else:
            print("请求方式错误,请检查!")
            res = None
        return res


if __name__ == "__main__":
    method = "GET"
    url = "https://www.jd.com"
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"}

    test_obj = run_test(method=method,url=url,headers=headers)
    response = test_obj.run_main()
    print(response.content.decode("utf-8"))

实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

如果对你有帮助的话,点个赞收个藏,给作者一个鼓励。也方便你下次能够快速查找。

如有不懂还要咨询下方小卡片,博主也希望和志同道合的测试人员一起学习进步

在适当的年龄,选择适当的岗位,尽量去发挥好自己的优势。

我的自动化测试开发之路,一路走来都离不每个阶段的计划,因为自己喜欢规划和总结,

测试开发视频教程、学习笔记领取传送门!!!

;