·设为首页收藏本站📧邮箱修改🎁免费下载专区💎积分购买✅卡密充值📒收藏夹🎖️众筹悬赏👽聊天室
返回列表 发布新帖

自己设置指定图片位置的代码,chatgpt写的

132 0
发表于 2023-6-24 10:54:56 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
import os
import random

def get_file_titles(folder_path):
    titles = []

    for filename in os.listdir(folder_path):
        if filename.endswith(".txt"):
            title = os.path.splitext(filename)[0]  # 使用文件名作为标题
            titles.append(title)

    return titles


def find_matching_keywords(titles, keywords_file):
    with open(keywords_file, 'r', encoding='utf-8') as file:
        keywords = [line.strip() for line in file]

    for title in titles:
        matched_keyword = None

        for keyword in keywords:
            if keyword in title:
                matched_keyword = keyword
                print(f"有关键词的标题: {title} (关键词: {keyword})")
                break

        if matched_keyword:
            # 生成随机数
            random_number = random.randint(1, 5)

            # 打开文件进行修改
            folder_path = '文本'
            file_path = os.path.join(folder_path, title + '.txt')
            with open(file_path, 'r', encoding='utf-8') as file:
                contents = file.read()

            # 查找第四个</p>标签的位置
            count = 0
            index = -1
            while count < 4:
                index = contents.find('</p>', index + 1)
                if index == -1:
                    break
                count += 1

            # 在第四个</p>标签之后插入'
                modified_contents = contents[:index + len('</p>')] + img_tag + contents[index + len('</p>'):]
                print(img_tag,"链接图片添加成功")
                print("-----------------------------------------------------------")

                # 将修改后的内容写回文件
                with open(file_path, 'w', encoding='utf-8') as file:
                    file.write(modified_contents)
        else:
            print(f"没有关键词的标题: {title}")
            print(f"-----------------------------------------------------------")

folder_path = '文本'
keywords_file = '关键词.txt'

titles = get_file_titles(folder_path)
find_matching_keywords(titles, keywords_file)
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

创宇盾启航版免费网站防御网站加速服务
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:3.145.81.9,GMT+8, 2024-9-20 09:03 , Processed in 0.320803 second(s), 76 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2024 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表