def scanfilepath Path content - dict htmlcontent = BeautifulSoupcontent htmlparser # 将p h2 h3 h4的内容去除符号之后作为搜索内容 textlist = join maplambda p resubu^u4e00-u9fa5u0030-u0039u0041-u005au006
def scanfile(path: Path, content) -> dict: 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": 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
原文地址: https://www.cveoy.top/t/topic/inkK 著作权归作者所有。请勿转载和采集!