import re from bs4 import BeautifulSoup import json from pathlib import Path from collections import deque

将root改成您存放html文件的目录

root = Path(".")

扫描html文件

def scanfile(path, content): htmlcontent = BeautifulSoup(content, 'html.parser') # 将p, h2, h3, h4的内容,去除符号之后作为搜索内容 textlist = "".join( map(lambda p: re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5])", "", p.get_text()), htmlcontent.find_all(name=['p', 'h2', 'h3', 'h4']))) # 以h1或者文件名作为标题 title = htmlcontent.find(name="h1") title = title.get_text() if title else path.stem print(title) return { # "title": title.replace("\n", ""), "title": re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5])", "", title), "path": path.relative_to(root).str().replace("\", "/"), "text": textlist }

if name == "main": j = [] target = deque([root])

# 递归的遍历文件夹下所有的html文件
while len(target) > 0:
    file = target.pop()
    if file.is_dir():
        target.extend(file.iterdir())
    elif file.is_file() and file.suffix == ".html":
        j.append(scanfile(file, file.read_bytes().decode('utf-8')))

# 将最后的扫描结果和search.js输出到searcher.js
# html文件中应该包含searcher.js
with open("searcher.js", "w") as output:
    with open("search.js", "r") as input:
        output.write("let SearchResult = '"+json.dumps(j)+"';\n")
        output.write(input.read()
import refrom bs4 import BeautifulSoupimport jsonfrom pathlib import Pathfrom collections import deque# 将root改成您存放html文件的目录root = Path# 扫描html文件def scanfilepath Path content - dict htmlcontent = Be

原文地址: https://www.cveoy.top/t/topic/ink5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录