找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 333|回复: 0

[接口自动化] requests模块的应用

[复制链接]

19

主题

3

回帖

79

积分

管理员

积分
79

热心会员湖北省RedHat高手

发表于 2025-6-21 12:00 | 显示全部楼层 |阅读模式
使pip install requestsrequests
LTXIT-20250621132747.jpg
PythonPOST
LTXIT-20250621135053.jpg
[Python] 纯文本查看 复制代码
import requests

url = "http://ip-api.com/batch"
data = [{"query": "119.88.88.88"}]
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0'}
resp = requests.post(url = url,json = data,headers = headers) #如果是get类型的请求,这里的“data = xxxx”需要修改为“params = xxxx”
print('请求的URL是:{}'.format(resp.request.url))
print('请求的参数是:{}'.format(resp.request.body))
print('请求的headers是:{}'.format(resp.request.headers))
print('请求的cookies是:{}'.format(resp.request._cookies))
print('响应码是:{}'.format(resp.status_code))
print('响应信息是:{}'.format(resp.text))
print('响应的headers是:{}'.format(resp.headers))
print('响应的cookies是:{}'.format(resp.cookies))


LTXIT-20250621140143.jpg
[Python] 纯文本查看 复制代码
import requests

session = requests.session() #实例化一个session
url = "http://ip-api.com/batch" #接口地址
data = [{"query": "119.99.119.99"}]
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0'}
def ip_query(request_method):
    resp = session.request(method = request_method,url = url,json = data,headers = headers) #如果是get类型的请求,这里的“data = xxxx”需要修改为“params = xxxx”
    print('请求的URL是:{}'.format(resp.request.url))
    print('请求的参数是:{}'.format(resp.request.body))
    print('请求的headers是:{}'.format(resp.request.headers))
    print('请求的cookies是:{}'.format(resp.request._cookies))
    print('响应码是:{}'.format(resp.status_code))
    print('响应信息是:{}'.format(resp.text))
    print('响应的headers是:{}'.format(resp.headers))
    print('响应的cookies是:{}'.format(resp.cookies))

ip_query('post') #将请求方式作为参数传入


LTXIT-20250621140939.jpg
[Python] 纯文本查看 复制代码
import requests

class IPQuery:
    def __init__(self,request_method):
        self.session = requests.session() #实例化一个session
        self.url = "http://ip-api.com/batch" #接口地址
        self.data = [{"query": "119.98.98.98"}]
        self.request_method = request_method
        self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0'}
    def ip_query(self):
        resp = self.session.request(method = self.request_method,url = self.url,json = self.data,headers = self.headers) #如果是get类型的请求,这里的“data = xxxx”需要修改为“params = xxxx”
        print('请求的URL是:{}'.format(resp.request.url))
        print('请求的参数是:{}'.format(resp.request.body))
        print('请求的headers是:{}'.format(resp.request.headers))
        print('请求的cookies是:{}'.format(resp.request._cookies))
        print('响应码是:{}'.format(resp.status_code))
        print('响应信息是:{}'.format(resp.text))
        print('响应的headers是:{}'.format(resp.headers))
        print('响应的cookies是:{}'.format(resp.cookies))

IPQuery('post').ip_query() #将请求方式作为参数传入

串类型的使json()可相应的数据类型(元祖、列表、字典等)
LTXIT-20250621143031.jpg
[Python] 纯文本查看 复制代码
import requests

class IPQuery:
    def __init__(self,request_method):
        self.session = requests.session() #实例化一个session
        self.url = "http://ip-api.com/batch" #接口地址
        self.data = [{"query": "119.98.98.98"}]
        self.request_method = request_method
        self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0'}
    def ip_query(self):
        resp = self.session.request(method = self.request_method,url = self.url,json = self.data,headers = self.headers) #如果是get类型的请求,这里的“data = xxxx”需要修改为“params = xxxx”
        print('响应信息①是:{}'.format((resp.text)))
        print('响应信息②是:{}'.format(resp.json()))
        print('响应信息①的数据类型是:{}'.format(type(resp.text)))
        print('响应信息②的数据类型是:{}'.format(type(resp.json())))

IPQuery('post').ip_query() #将请求方式作为参数传入

评分

参与人数 1ITB +50 贡献值 +5 收起 理由
Happy + 50 + 5 内容专业易懂

查看全部评分

Archiver|手机版|小黑屋|LTXIT家园 ( 鄂ICP备2025089526号|鄂公网安备42110002000175号 )

GMT+8, 2025-8-3 00:52 , Processed in 0.138724 second(s), 28 queries .

Powered by LTXIT家园 X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表