# encoding:utf-8
import mysql.connector # 先安装mysql-connector-python-1.0.12-py3.3,再引入包
import random
import re
import codecs
import json
import requests
import time
import os, pprint
import os
from aip import AipNlp
dir_name = str(input("请输入生成文件夹名称:"))
# page_num = int(input("请输入生成文章篇数:"))
# dir_name = 'test'
all_link = '/Users/lilong/Desktop/{}'.format(dir_name)
dir = os.makedirs(all_link)
config = {'host': '127.0.0.1',
'user': 'root',
'password': 'yz1028959',
'port': 3306,
'database': 'data_likeshuo',
'charset': 'utf8'
}
try:
cnn = mysql.connector.connect(**config) # connect方法加载config的配置进行数据库的连接,完成后用一个变量进行接收
except mysql.connector.Error as e:
print('数据库链接失败!', str(e))
else: # try没有异常的时候才会执行
print("sucessfully!")
cursor = cnn.cursor(buffered=True) # 获取查询的标记位
######接口#######
APP_ID = ''
API_KEY = '
SECRET_KEY = ''
client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
maxSummaryLen = 300
######分割线#####
""" 调用新闻摘要接口 """
def lks_db(xx):
content_top = xx
content_top = content_top.replace(u'\xa0', u'')
options = {}
options["title"] = "标题"
# """ 带参数调用新闻摘要接口 """
Res = client.newsSummary(content_top, maxSummaryLen, options)
db_top = Res['summary']
return db_top
# 查询数据库
n = random.randint(2, 4) # 随机生成自然段
print("成功生成的每篇文章3-5自然段") # 随机生成自然段+导读自然段
data = (input("请输入关键词:"), n)
# data = ("英语", n)
t_key = data[0] # 只取关键词,不取随机生成自然段
cx_sql = 'select t1.content from children_title t LEFT JOIN children_english t1 on t.ceid=t1.id where t.ckey=%s ORDER BY RAND() LIMIT %s'
s_title = "select title from lks_title where title like '%%%%%s%%%%' ORDER BY RAND() LIMIT 1" % t_key
content_page_num = int(input("请输入生成文章篇数:"))
page_num = 1
article = 0
while True:
if article == content_page_num:
break
cursor.execute(cx_sql, data, )
all = cursor.fetchall()
cursor.execute(s_title)
rand_title = cursor.fetchall()
rand_title = rand_title[0][0] # 在列表中的元组取出字符串
# print(rand_title) # 打印标题
# print(all) # 打印内容(随机生成的n段内容)
p1 = re.compile(r'\s+') # 正则
add_list = [];
content_list = []
for i in all:
content = ''
tupe1 = i[0]
add_list.append(tupe1)
content += i[0]
content = p1.sub('', content) # 去除让内容空格
content_list.append(content) # 新内容添加到content_list
# print(content_list) # 内容中部装在一个列表[]里面,以字符串,分开
n_piecewise_content = ''
for i in content_list:
n_piecewise_content += ' ' + i + '\r\n'
center_title = rand_title.center(100) + '\r\n' # 文章标题居中
header = " " + lks_db(n_piecewise_content) + '\r\n' # 文章第一自然段,概括内容(对n_piecewise_content总结)
middle = n_piecewise_content # 文章中部
all_text = center_title + header + middle # 标题+开头+中部
# print(all_text)
# for j in range(1, 2):
one_content = all_text + '\r\n'
# print(one_content)
txt = os.path.join(all_link, str(article+1) + '.txt')
w_txt = open(txt, 'w')
f = codecs.open(txt, 'r+', encoding='utf-8')
# print(f)
f.write(one_content)
f.close()
article += 1
网友评论