前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在html文件的指定位置加入指定文本

在html文件的指定位置加入指定文本

作者头像
南锋
发布2024-08-07 18:38:31
670
发布2024-08-07 18:38:31
举报
文章被收录于专栏:淡忘的博客

记录自己工作中用到的脚本,因为我们的cocosCreator项目导出web项目后,需要修改index.html文件,每次手动修改都很麻烦,而且容易出错,于是决定用脚本来搞定。 我这里是用python写的,python版本为3.8

(adsbygoogle = window.adsbygoogle || []).push({});

要在 HTML 文件的指定位置插入指定的文本,可以使用 PythonBeautifulSoup 库。

安装库

首先,安装 BeautifulSouplxml

代码语言:javascript
复制
pip3 install BeautifulSoup

代码

我这里是在index.html中的<head><body>中添加了一些代码。完整代码如下:

代码语言:javascript
复制
from bs4 import BeautifulSoup

def insert_code_in_html(file_path):
    head_code = '''
    <script src="https://telegram.org/js/telegram-web-app.js"></script>
    <style>
        body, html {
            height: 100%;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        #GameDiv {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
        }

        #Cocos3dGameContainer {
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        #GameCanvas {
            width: 100%;
            height: 100%;
        }

        #loading-animation {
            display: flex;
            justify-content: center;
            align-items: center;
            position: absolute;
            top: calc(50% - 200px);
            left: 50%;
            transform: translate(-50%, -50%);
        }

        #loading-text {
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-size: 20px;
            position: absolute;
            top: calc(50% - 80px);
            left: 50%;
            transform: translate(-50%, -50%);
            width: 80%;
            text-align: center;
        }
    </style>
    '''
    body_code = '''
    <div id="loading-animation">
        <img src="web.png" alt="Loading..." style="width: 100%; height: 100%;">
    </div>
    <div id="loading-text">Loading may take a few seconds for the first time. Please be patient.</div>
    <script>
        const tg = window.Telegram.WebApp;
        tg.isClosingConfirmationEnabled = true;
    </script>
    '''

    # 读取 HTML 文件内容
    with open(file_path, 'r', encoding='utf-8') as file:
        soup = BeautifulSoup(file, 'lxml')

    # 在 <head> 中插入代码
    if soup.head:
        soup.head.append(BeautifulSoup(head_code, 'html.parser'))
    else:
        print("<head> 标签未找到。")

    # 在 <body> 中插入代码
    if soup.body:
        soup.body.append(BeautifulSoup(body_code, 'html.parser'))
    else:
        print("<body> 标签未找到。")

    # 将修改后的 HTML 写回文件
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(str(soup))

# 示例用法
file_path = 'web-mobile/index.html'
insert_code_in_html(file_path)

由于自己太懒,连代码都不想运行,于是乎,直接将上面python脚本导出了一个.exe可执行文件,每次只要双击改执行文件就行了。

踩坑

运行的时候报下面错:

代码语言:javascript
复制
Traceback (most recent call last):
  File ".\html.py", line 1, in <module>
    from bs4 import BeautifulSoup
  File "C:\Users\84267\AppData\Roaming\Python\Python38\site-packages\bs4\__init__.py", line 37, in <module>
    from .builder import (
  File "C:\Users\84267\AppData\Roaming\Python\Python38\site-packages\bs4\builder\__init__.py", line 9, in <module>
    from bs4.element import (
  File "C:\Users\84267\AppData\Roaming\Python\Python38\site-packages\bs4\element.py", line 13, in <module>
    from bs4.formatter import (
  File "C:\Users\84267\AppData\Roaming\Python\Python38\site-packages\bs4\formatter.py", line 1, in <module>
    from bs4.dammit import EntitySubstitution
  File "C:\Users\84267\AppData\Roaming\Python\Python38\site-packages\bs4\dammit.py", line 12, in <module>
    from html.entities import codepoint2name
  File "E:\Project_D_tg\build\html.py", line 1, in <module>
    from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup' from partially initialized module 'bs4' (most likely due to a circular import) (C:\Users\84267\AppData\Roaming\Python\Python38\site-packages\bs4\__init__.py)

如下图:

解决方案: python文件名的问题,因为我的python脚本命名为html.py,这里的html和代码里面的html冲突,所以导致报错,这里只需要修改python的文件名即可。

好吧,表示自己第一次遇到这种因为文件名报错的情况。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装库
  • 代码
  • 踩坑
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档