簡體   English   中英

使用c#從Web瀏覽器識別內容

[英]Identifying the content from the Web Browser using c#

我使用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();
    }

我唯一的問題是,如何將字符串替換為另一個單詞(特別是將其更改為星號(*))而不是突出顯示它?

更改:

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

至:

html.Replace(key, "*");

或者,如果您試圖用星號來空白單詞的每個特定字符,則構建一個與key的長度一樣長的星號字符串(您可以使用key.Length的值來key.Length )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM