base_moudle.py
日志文件输出,使用装饰器获取响应时间
import functools
import time
import logging
import os
log_file = os.path.join(os.getcwd() + "/request.log")
logging.basicConfig(level=logging.INFO,
filename=log_file,
datefmt='%Y-%m-%d %H:%M:%S',
format='%(asctime)s %(levelname)s %(module)s %(message)s')
Logger = logging.getLogger(__name__)
def run_time(func):
@functools.wraps(func)
def wraps(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
end = time.perf_counter()
Logger.info("使用装饰器方法:{}, 响应时间:{}ms, 输出的结果:{}".format(func.__name__, int((end - start) * 1000), result))
return wraps
抓取数据
import json
import xlwt
from tests.base_module import Logger,run_time
import requests
def get_userData(url, headers, cookies):
response = requests.get(url, headers=headers, cookies=cookies)
response = response.text
data = json.loads(response)
Logger.info(data)
userList = data["data"]["userList"]
user = []
for userData in userList:
dict(userData)
user_id = userData["user_id"]
peiyou_name = userData["name"]
tuple1 = [user_id, peiyou_name]
user.append(tuple1)
return user
# 设置表格样式
def set_style(name, height, bold=False):
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = name
font.bold = bold
font.color_index = 4
font.height = height
style.font = font
return style
# 写Excel
@run_time
def write_excel():
f = xlwt.Workbook()
sheet1 = f.add_sheet('用户', cell_overwrite_ok=True)
row0 = ["user_id", "姓名"]
user_list = get_userData(url, headers, cookies)
# 写第一行
for i in range(0, len(row0)):
sheet1.write(0, i, row0[i], set_style('Times New Roman', 220, True))
# 写第二行
a = 0
for user in user_list:
print(user)
a += 1
for i in range(0, len(user)):
sheet1.write(a,i, str(user[i]), set_style('Times New Roman', 220, True))
f.save('test.xls')
if __name__ == "__main__":
url = "https://simple.test.com/api/student/studentinfo?course_id=1000&class_id=-1&course_state=2"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/80.0.3987.122 Safari/537.36"
}
cookies = {
"Cookies": "示例cookie"}
# get_userData(url, headers, cookies)
write_excel()
超越
网友评论