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"))

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

你可能感兴趣的:(软件测试,python,开发语言,功能测试,软件测试,自动化测试,程序人生,职场和发展)