每周

作者: 月夜星空下 | 来源:发表于2020-10-16 15:16 被阅读0次
    # encoding:utf-8
    import os
    import re
    import json
    import time
    import random
    # import json
    import codecs
    import pymysql
    import requests
    import os, pprint
    import mysql.connector  # 先安装mysql-connector-python-1.0.12-py3.3,再引入包
    # from aip import AipNlp
    ###图片路径###
    path_url = '/Users/lilong/Desktop/likeshuo/url.txt'
    op_url = open(path_url, encoding='utf-8')
    pool_op_url = list(op_url)
    #############
    # data = readJSON.read_JSON("data.json")
    # non = data['bosh'] #代表文章主要废话来源
    # duplication = 1
    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 = '17895362'
    # API_KEY = 'LDvWo01ypc5umRTk6B3q1hjK'
    # SECRET_KEY = 'ywEQtOsx4WVVPg291Y9rmOA7uqG9GDcs'
    # client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
    # maxSummaryLen = 300
    ######分割线#####
    """ 调用新闻摘要接口 """
    
    # 查询数据库
    n = random.randint(3, 5)  # 随机生成自然段
    # print("「简版」—— 用于测试环境")  # 随机生成自然段+导读自然段
    # data = (input("请输入关键词:"), n)
    data = (input("请输入关键词:"), 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
    # link = pymysql.Connect(host='localhost', port=3306, user='root', password='yz1028959', db='dede1', charset='utf8')
    # cursorde = link.cursor()
    count_dd = 'select count(1) from dede_addonarticle'
    # cursorde.execute(count_dd)
    # rand_x = cursorde.fetchall()[0][0]
    # cursorde.close()
    # link.close()
    # print(rand_x)
    
    content_page_num = int(input("请输入生成文章篇数:"))
    page_num = 1
    article = 0
    all_rand_title = []
    
    # all_n_piecewise_content = []
    # all_one_content = []
    p_lable = "<p>"
    nbsp = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
    p_lable_last = "</p>"
    all_one_middle = []
    # randint_x = rand_x + content_page_num
    randint_x = content_page_num
    all_list_con_all = []
    while True:
        if article == content_page_num:
            break
    
        a_num = random.randint(0, randint_x)
        all_con_str = ''
        all_list_con = []
        random.shuffle(pool_op_url)
        con_url = pool_op_url[0]
    
        # href_a = 'Click on the surprise:<body><a href="http://127.0.0.1/baike/{}.html" target="_blank">{}</a></body>'.format(a_num, t_key)
        cursor.execute(cx_sql, data, )
        all = cursor.fetchall()
        cursor.execute(s_title)
        rand_title = cursor.fetchall()
        rand_title = rand_title[0][0]  # 在列表中的元组取出字符串
        all_rand_title.append(rand_title)
    
        # con_url = "<p style={center}><img src={url}  alt={rt}></p>".format(center="text-align:center", url=con_url, rt=rand_title )
    
        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 += p_lable + nbsp + i + p_lable_last + '\r\n'
            n_piecewise_content += "   " + i + '\r\n'
            # header = "   " + str(tmp) + lks_db(n_piecewise_content) + '\r\n'  # 文章第一自然段,概括内容(对n_piecewise_content总结)
    
            center_title = rand_title.center(100) + '\r\n'  # 文章标题居中
            # middle = con_url + n_piecewise_content + href_a  # 文章中部
            middle = n_piecewise_content
            # middle = n_piecewise_content
            # headers = {'Content-Type': 'application/x-www-form-urlencoded',
            #            'Authorization': '7DFC27E2A121441A9C9CF348CC202D03'}  # 消息头,根据实际需要添加
            # url = "http://apis.5118.com/wyc/title"  # 地址
            # print('1')
            # body = middle
            # Body = {'txt': body}  # 入参
            # res = requests.post(url, headers=headers, data=Body)  # 接口调用
            # resTest = json.loads(res.text)
            # rand_title = resTest['data']
            # center_title = rand_title.center(100) + '\r\n'
            # all_text = center_title + header + middle  # 标题+开头+中部
            all_text = center_title + middle
    
        all_one_middle.append(middle)
    
        one_content = all_text + '\r\n'
    
    ###生成###
        # txt = os.path.join(all_link, str(article + 1) + '.txt')
        txt = os.path.join(all_link, rand_title + '.txt')
        w_txt = open(txt, 'w')
        f = codecs.open(txt, 'r+', encoding='utf-8')
        f.write(one_content)
        f.close()
        article += 1
    print('ok')
    
    

    相关文章

      网友评论

          本文标题:每周

          本文链接:https://www.haomeiwen.com/subject/qsizpktx.html