帝国cms发布内容python脚本(excl表格内容发布到网站帝国cms)

发布时间 : 07-17
帝国cms发布内容python脚本(excl表格内容发布到网站帝国cms)



# -*- coding: utf-8 -*-

'''
@Time    : 2021/7/3 20:18
@Author  : junge
@FileName: excl_diguocms.py
@Software: PyCharm
 
'''
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from threading import Thread
import random
from queue import Queue
from jieba.analyse import tfidf
import requests
import re
import time


class ExcelPoster(Thread):
    def __init__(self, config, queue):
        super().__init__()
        self.config = config
        self.queue = queue

    def run(self):
        while True:
            title, content = self.queue.get()
            self.post(title, content)
            self.queue.task_done()

    def post(self, title, content):
        form = {
            'username': 'admin',  # 填写自己的用户名
            'enews': 'AddNews',  # 添加新闻
            'classid': self.config['catid'],  # 分类id
            'bclassid': 0,  # 父分类id
            'id': 0,  # 文章id
            'ecmsnfrom': 1,
            'title': title,
            'checked': 1,
            'keyboard': ",".join(tfidf(title, topK=3)),  # tag标签
            'newstime': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
            'smalltext': re.sub(r'(</?[^>]+>|\s)', '', content)[:80],
            'writer': 'xiaobai',
            'befrom': '互联网',  # 来源
            'newstext': content,  # 内容
            'dokey': 1,  # 关键字替换
            'copyimg': 1,  # 远程保存图片
            'autosize': 5000,
            'getfirsttitlepic': 1,
            'getfirsttitlespicw': 105,
            'getfirsttitlespich': 118,
            'istop': 0,
            'newspath': time.strftime('%Y-%m-%d', time.localtime()),  # 文件路径
            'addnews': '提交'
        }
        try:
            r = requests.post(self.config['api'], data=form, timeout=10)
        except requests.RequestException as err:
            print(f"post {title} error: {err}")
        else:
            print(r.text)


def read_data_from_excel(sheet, queue):
    for row in sheet.iter_rows():
        r = [x.value for x in row][:2]
        queue.put(r)


if __name__ == "__main__":
    article_queue = Queue()
    # 要发布的excel文件,必须只能存在标题和内容两栏
    wb = load_workbook(r"D:\Python\python38\搜狗问答\benchi.xlsx")
    wsheet = wb.active
    # 配置
    post_config = {
        "api": "http://www.xxxxxx.com/e/admin/jiekou_v7.5.php?pw=123456",  # phpcms发布接口地址
        "catid": 3  # 要发布的栏目ID
    }
    read_data_from_excel(wsheet, article_queue)
    for _ in range(1):
        ep = ExcelPoster(post_config, article_queue)
        ep.daemon = True
        ep.start()

    article_queue.join()
    print("done")

文章推荐:

帝国cms发布内容python脚本(excl表格内容发布到网站帝国cms)