使用pip install requests命令安装requests模块
Python调用POST接口
[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))
将接口请求方式作为一个参数传入(普通函数)
[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') #将请求方式作为参数传入
将接口请求方式作为一个参数传入(类与对象)
[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()可直接转换成为相应的数据类型(元祖、列表、字典等)
[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() #将请求方式作为参数传入
|