复制代码, 保存为.py文件, 在需要使用的模块调用即可(注意文件位置修改), 日志器内容参考上一篇文章, 需要引入的第三方库请自行下载…
import os
import json
from bs4 import BeautifulSoup
from utils.get_log import GetLog
# 获取日志器
log = GetLog.get_log()
def read_json(file_name):
"""
读取json文件为python字典对象
:param file_name: 文件名称(不含后缀)
:return: 字典对象
"""
try:
with open("\\data\\{}.json".format(file_name), "r", encoding="utf-8") as f:
result = json.load(f)
return result
except Exception as ex:
log.error(ex)
def write_json(data_source, file_name):
"""
写入python字典对象为json文件
:param data_source: 字典对象
:param file_name: 保存的文件名(不含后缀)
:return: json文件
"""
try:
with open("\\data\\{}.json".format(file_name), "w", encoding="utf-8") as f:
# # 先序列化, 解决默认ascii编码, 解决中文乱码问题
# data = json.dumps(data_source, ensure_ascii=False, indent=4)
json.dump(data_source, f, ensure_ascii=False)
except Exception as ex:
log.error(ex)
def read_txt(file_name):
"""
读取txt文件为python列表对象, txt每行内容为列表的一个元素
:param file_name: 要读取的文件名(不含后缀)
:return: 列表对象
"""
try:
filepath = "\\data\\{}.txt".format(file_name)
with open(filepath, "r", encoding="utf-8") as f:
list_data = f.readlines()
return [x.strip() for x in list_data]
except Exception as ex:
log.error(ex)
def write_txt(data_source, file_name):
"""
保存数据(字符串)到txt文件(追加形式)
:param data_source: 字符串
:param file_name: 保存的文件名(不含后缀)
:return: txt文件
"""
try:
filepath = "\\data\\{}.txt".format(file_name)
with open(filepath, "a+", encoding="utf-8") as f:
f.write(str(data_source) + '\n')
except Exception as ex:
log.error(ex)
def save_html(dir_name, file_name, file_content):
"""
保存html文件
:param dir_name: 设置保存的位置
:param file_name: 设置保存的文件名(不含后缀)
:param file_content: 保存的内容(例如接口返回的html文本-[res.text])
:return: html文件
"""
try:
path = "\\data\\{}".format(dir_name)
if not os.path.exists(path):
os.makedirs(path)
with open("{}\\{}.html".format(path, file_name), "w", encoding="utf-8") as f:
f.write(file_content)
except Exception as ex:
log.error(ex)
def read_html(dir_name, file_name):
"""
读取html文件
:param dir_name: 文件路径名
:param file_name: 文件名(不含后缀)
:return: bs4对象(通常配合bs4使用)
"""
try:
with open("\\data\\{}\\{}.html".format(dir_name, file_name), "r", encoding="utf-8") as f:
return BeautifulSoup(f, 'lxml')
except Exception as ex:
log.error(ex)