requests初识
import requests response=requests.get('http://www.baidu.com') # print(response.status_code) #返回状态码 # print(response.url) #返回URL # print(response.text) #返回文本数据类型 会有乱码 # print(response.encoding) #ISO-8859-1返回的编码格式 # response.encoding='utf-8' #设置编码格式 # print(response.text) # print(response.cookies) #返回的cookies requests.request(method='get',url='http://www.baidu.com') #自定义请求方式格式 # print(response.status_code) # print(response.content) #返回的二进制数据,比如视频图片类型的数据 # print(response.headers) #返回的响应头 # print(response.json()) #返回json数据的格式 #爬取图片 # response=requests.request('get', # 'https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00562-1301.jpg' # ) # print(response.status_code) # print(response.content) # with open('美图.png','wb')as f: # f.write(response.content) response=requests.request('get', 'https://vd3.bdstatic.com/mda-mii9ravnin3pfa28/sc/cae_h264_nowatermark/1632328435102546038/mda-mii9ravnin3pfa28.mp4?v_from_s=hkapp-haokan-nanjing&auth_key=1654743114-0-0-d92cfcca5f88b556c3795dd2e90f56fb&bcevod_channel=searchbox_feed&pd=1&cd=0&pt=3&logid=1314589039&vid=10892099404777121974&abtest=102599_1-102777_4-102784_2-17451_1-3000232_2-3000233_3&klogid=1314589039') with open('新宝岛.mp4','wb')as f: for i in response.iter_content(chunk_size=1024): f.write(i)
cms演示
import requests def send_request(): url='http://124.220.179.221:8081/cms/manage/loginJump.do' data={ "userAccount":"admin", "loginPwd":"123456", } response=requests.request(method='post',url=url,data=data) # print(response.status_code) # print(response.headers) print(response.json()) return response.cookies ret = send_request() def add_user(ret): url='http://124.220.179.221:8081/cms/manage/saveSysUser.do' data={ "userName": "狗蛋", "userSex": "1", "userMobile": "18934335555", "userEmail": "43267467@qq.com", "userAccount": "ggggg", "loginPwd": "1000011", "confirmPwd": "100011" } response=requests.request('post',url, data =data,cookies=ret) print(response.status_code) print(response.headers) print(response.text) add_user(ret)
参数介绍
import requests # get 请求 # response=requests.get('http://www.httpbin.org/get?k1=v1&k2=v2') # print(response.status_code) # print(response.headers) # print(response.json()) # params={ # "k1": "v1", # "k2": "v2" # } # response=requests.get('http://www.httpbin.org/get',params=params) # print(response.status_code) # print(response.json()) #post # 以下两种请求头都用data传参 # data 请求头的类型改为默认x-www-form-urlencoded # json 如果用json传参会把请求头的类型改为默认application/json # cms登录接口 # url='http://124.220.179.221:8081/cms/manage/loginJump.do' # data={ # "userAccount":"admin", # "loginPwd":"123456", # } # response=requests.post(url=url,data=data) # print(response.status_code) # print(response.headers) # print(response.json()) # get请求和post 其实属于简单请求 # 复杂请求:put delter patch option head # 复杂请求涉及到跨域的问题,遵循的是restfuiapi规范 # requests.request()
xrld模块
# # xlrd 和openpyxl # # 两者都是对于excel文件的操作插件,两者的主要区别在于写入操作,其中xlwt针对Ecxec2007之前的版本,即.xls文件,用xlrd读取,用openpyxl写入。 import xlrd import xlwt from xlutils import copy import requests import json # class ExcelSet: # # def __init__(self): # self.excel_path='test.xls' # 文件名以及路径,如果路径或者文件名有中文给前面加一个r拜师原生字符。 # self.work_book = xlrd.open_workbook(self.excel_path) # self.work_sheet = self.work_book.sheets() #获取所有的sheet # # print(self.work_sheet) # # # self.work_sheet=self.work_book.sheet_by_index(0) #通过索引顺序获取 # # print(self.work_sheet) # # # self.work_sheet = self.work_book.sheet_by_name('示例') #通过名称获取 # # print(self.work_sheet) # # 以上三个函数都会返回一个xlrd.sheet.Sheet()对象 # # # content=self.work_book.sheet_loaded('示例') # 检查某个sheet是否导入完毕 # # print(content) # # # #行的操作 # # nrows= self.work_sheet.nrows # 获取该sheet中的有效行数 # # print(nrows) # # # # row_lst=self.work_sheet.row(0) # 返回由该行中所有的单元格对象组成的列表 # print(row_lst) # # # # table.row_slice(rowx) # 返回由该列中所有的单元格对象组成的列表 # # # # table.row_types(rowx, start_colx=0, end_colx=None) # 返回由该行中所有单元格的数据类型组成的列表 # # # # table.row_values(rowx, start_colx=0, end_colx=None) # 返回由该行中所有单元格的数据组成的列表 # # # # table.row_len(rowx) # 返回该列的有效单元格长度 # # # # # # # # # # ExcelSet() def login(): url = 'http://124.220.179.221:8081/cms/manage/loginJump.do' data = { "userAccount": "admin", "loginPwd": "123456" } response =requests.request(method='post', url=url, data=data) return response.cookies class ExcelSet: def __init__(self,login): self.excel_path = 'test.xls' # 文件名以及路径,如果路径或者文件名有中文给前面加一个r拜师原生字符。 self.work_book = xlrd.open_workbook(self.excel_path,formatting_info=True) #将新表的格式和原表一样 self.work_sheet=self.work_book.sheet_by_name('示例') self.cookies=login() self.new_work_book=copy.copy(self.work_book) self.new_work_sheet=self.new_work_book.get_sheet(0) def read_excel_data(self): for row in range(1,self.work_sheet.nrows): #从第一行开始循环到有效行数以内行 case_id=self.work_sheet.cell_value(row,0) desc = self.work_sheet.cell_value(row, 1) url = self.work_sheet.cell_value(row, 2) method = self.work_sheet.cell_value(row, 3) header = self.work_sheet.cell_value(row, 4) data = self.work_sheet.cell_value(row, 5) except_result = self.work_sheet.cell_value(row, 6) response_result = self.work_sheet.cell_value(row, 7) my_test_case=MyTestCase(case_id,desc,url,method,header,data,except_result,response_result,self.cookies) ret=my_test_case.test_01_cms() if ret.get('msg')==eval(except_result).get('msg'): print('成功') self.write_excel_data(row,'测试成功') else: print('失败') self.write_excel_data(row,'测试失败') def write_excel_data(self,row,msg): self.new_work_sheet.write(row,7,msg) self.new_work_book.save('result.xls') class MyTestCase: def __init__(self,case_id,desc,url,method,header,data,except_result,response_result,cookies): self.case_id=case_id self.desc=desc self.url=url self.method=method self.header=header self.data=eval(data) self.except_result=except_result self.response_result=response_result self.cookies=cookies def test_01_cms(self): response=requests.request(method=self.method,url=self.url,data=self.data,cookies=self.cookies) return response.json() # if __name__=='__main__': e=ExcelSet(login) e.read_excel_data() token接口
import requests class TookMsg: def __init__(self): self.base_url="http://49.234.14.39:5001/" url = '/user/login' data = { "password": "123456", "username": "admin" } response = requests.request(method='post', url=self.base_url+url,json=data) self.token=response.json().get('access_token') print('self.token',self.token) def user_query(self): url='/user/query' data={ "access_token": self.token } response=requests.request(method='post',url=self.base_url+url,json=data) print(response.status_code) print(response.json()) def user_init(self): url='/user/init' data={ "access_token": self.token } response = requests.request(method='post', url=self.base_url + url, json=data) print(response.status_code) print(response.json()) def user_delete(self): url='/user/delete' data={ "access_token": self.token, "username": "user6" } response = requests.request(method='post', url=self.base_url + url, json=data) print(response.status_code) print(response.json()) t= TookMsg() t.user_query() t.user_delete() t.user_init()