把html转化成string代码

本文介绍了一种将HTML格式的字符串转换为纯文本的方法,包括去除HTML标签、脚本、样式,过滤特殊标签,并进行文本格式优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static String htmlToText(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;


        java.util.regex.Pattern p_html1;
        java.util.regex.Matcher m_html1;


        try {
            String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[//s//S]*?<///script>
            String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[//s//S]*?<///style>
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
            String regEx_html1 = "<[^>]+";
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签


            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签


            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签


            p_html1 = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE);
            m_html1 = p_html1.matcher(htmlStr);
            htmlStr = m_html1.replaceAll(""); // 过滤html标签


            textStr = htmlStr;


        } catch (Exception e) {
            // System.err.println("Html2Text: " + e.getMessage());
        }
        // 特殊标签过滤
        if (textStr.contains("&ldquo;")) {
            textStr = textStr.replace("&ldquo;", "“");
        }
        if (textStr.contains("&rdquo;")) {
            textStr = textStr.replace("&rdquo;", "”");
        }
        if (textStr.contains("&nbsp;")) {
            textStr = textStr.replace("&nbsp;", "");
        }
        if (textStr.contains("&nbsp;")) {
            textStr = textStr.replace("&nbsp;", "");
        }
        if (textStr.contains("<br/>")) {
            textStr = textStr.replace("<br/>", "");
        }
        if (textStr.contains("\n")) {
            textStr = textStr.replace("\n", "");
        }


        return textStr;// 返回文本字符串
    }
当然可以!将HTML代码转换为Python通常意味着你想在Python中生成或操作HTML内容。你可以使用一些库来帮助你完成这个任务,比如BeautifulSoup、lxml或者直接使用字符串操作。 假设你有如下的HTML代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Sample Page</title> </head> <body> <h1>Welcome to the Sample Page</h1> <p>This is a sample paragraph.</p> </body> </html> ``` 你可以使用Python来生成类似的HTML结构。以下是一个简单的例子,使用字符串操作来生成HTML: ```python html_content = """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{title}</title> </head> <body> <h1>{header}</h1> <p>{paragraph}</p> </body> </html> """.format(title="Sample Page", header="Welcome to the Sample Page", paragraph="This is a sample paragraph.") print(html_content) ``` 如果你想要更灵活和强大的HTML生成方式,可以使用BeautifulSoup库。首先,你需要安装BeautifulSoup和lxml: ```sh pip install beautifulsoup4 lxml ``` 然后你可以这样使用BeautifulSoup来生成HTML: ```python from bs4 import BeautifulSoup # 创建一个新的BeautifulSoup对象 soup = BeautifulSoup("<!DOCTYPE html><html lang='en'></html>", 'html.parser') # 添加head部分 head = soup.new_tag('head') soup.html.append(head) meta = soup.new_tag('meta', charset='UTF-8') head.append(meta) title = soup.new_tag('title') title.string = "Sample Page" head.append(title) # 添加body部分 body = soup.new_tag('body') soup.html.append(body) h1 = soup.new_tag('h1') h1.string = "Welcome to the Sample Page" body.append(h1) p = soup.new_tag('p') p.string = "This is a sample paragraph." body.append(p) # 打印生成的HTML内容 print(soup.prettify()) ``` 这段代码会生成与之前相同的HTML内容,但使用了BeautifulSoup来动态构建HTML结构。 希望这对你有帮助!如果你有更多问题或需要进一步的帮助,请告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值