需要一个json格式的模型库,算是半成品,思路可以借鉴,
# 创建时间 : 2021/8/16
# 作者 : 一本叔
import json
import os
import random
import re
class Seo:
def __init__(self, keyword, jsonfile):
self.keyword = keyword
self.jsonfile = jsonfile
# 读取json文件随机数据
def fromwork(self, *args):
text = json.load(open(self.jsonfile, 'r', encoding='utf-8'))
model_text = list(random.choice(text['早教']['model']).values())[0]
head_text = random.choice(text['早教']['heads'])
centent = model_text + '\n' + head_text
cizu = random.choice(text['早教']['词组']['名词'])
return head_text, cizu
def bt(self, *args):
new_txt = self.fromwork()
texts = 'C:\\Users\\Administrator\\Desktop\\SEO文章生成\\临时库.txt'
new_txts = new_txt[1]
with open(texts, 'w', encoding='utf-8') as f:
f.write(new_txt[0])
with open(texts, 'w', encoding='utf-8') as f:
dd = f.read()
for i in [*args]:
with open(texts, 'w', encoding='utf-8') as f:
dd =f.read()
# uu =dd.repleace(i, new_txts)
print(dd)
# # 创建桌面目录文件夹
try:
os.makedirs(str(os.path.join(os.path.expanduser('~'), "Desktop", 'SEO文章生成')))
except FileExistsError:
print('文件夹已创建')
jsonfile = 'C:\\SEO文章生成\\模型库.json'
seo = Seo('j', jsonfile)
# seo.fromwork()
seo.bt('【宝宝】', '【儿童】', '【早教班】')
# s = seo.bt()