Python+Requests simulates sending GET requests

Simulate sending a GET request

Precondition: import requests library

1. Send a get request without parameters

code show as below:

Take Baidu homepage as an example

import requests 

# Send get request 
response = requests.get(url="http://www.baidu.com") 
print(response.content.decode("utf-8")) # Output content in utf-8 encoding

2. Send a get request with parameters

There are several ways to send a get request with parameters

Method 1: The parameters are in the URL

code show as below:

Take Baidu homepage as an example

import requests 

# Send get request with parameters 
# Method 1: Parameters in URL 
# http protocol, www.baidu.com host number, /s request address, wd=cat parameter 
host = "http://www.baidu.com /s?wd=cat" 
# Because the Baidu server will check the header information, you need to add the request header 
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"))

Method 2: The parameters are in the dictionary

code show as below:

Take Baidu homepage as an example

import requests 

# Method 2: The parameters are in the dictionary 
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':'Leopard'} # parameters in the dictionary 
response = requests.get(url=host,headers=headers,params=data) 
print(response. content. decode('utf-8'))

Get the basic information of the response data

code show as below:

Take Baidu homepage as an example

import requests 

# Send a get request 
response = requests.get(url="http://www.baidu.com") 
# Get the basic information of the response 
print("Status code:", response.status_code) 
print("Request URL: ",response.url ) 
print("Header information:", response.headers) 
print("Cookie information:", response.cookies) 
print("Byte information:",response.content) 
print("Text information : ",response.text) 
print("Encoding format:",response.encoding)

Encapsulate get request

Code example:

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

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

Encapsulate the main method

Code example:

# 封装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"))

Encapsulation test class

Code example:

# 将写好的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"))

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/130061047