使用c#从Web浏览器识别内容

问题描述 投票:0回答:1

我使用Web浏览器控件创建了它,其中只识别代码中指示的特定单词。唯一的功能是,它可以读取内容并突出显示网页内容中的单词。我唯一的问题是,如何将字符串替换为另一个字(特别是将其更改为星号(*))而不是突出显示它?谢谢。

 private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate(txbAdress.Text);
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        IHTMLDocument2 doc2 = webBrowser1.Document.DomDocument as IHTMLDocument2;
        StringBuilder html = new StringBuilder(doc2.body.outerHTML);

        var words = new[] { "bobo", "tanga", "gago" };
        foreach (String key in words)
        {
            String substitution = "<span style='background-color: rgb(255, 0, 0);'>" + key + "</span>";
            html.Replace(key, substitution);
        }

        doc2.body.innerHTML = html.ToString();
    }
c# replace webbrowser-control substitution mshtml
1个回答
0
投票

我唯一的问题是,如何将字符串替换为另一个字(特别是将其更改为星号(*))而不是突出显示它?

更改:

String substitution = "<span style='background-color: rgb(255, 0, 0);'>" + key + "</span>";
html.Replace(key, substitution);

至:

html.Replace(key, "*");

或者,如果你试图用星号清除单词的每个特定字符,那么只要key的长度(你可以找到key.Length的值)就可以构建一串星号。

© www.soinside.com 2019 - 2024. All rights reserved.