我正在编写这段代码,它可以在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);
string query;
query = @"select Word from ListWords";
List<string> words = new List<string>();
DataSet ds;
DataRow drow;
ds = DatabaseConnection.Connection1(query);
int index, total;
total = ds.Tables[0].Rows.Count;
string current_word;
for (index = 0; index < total; index++ )
{
drow = ds.Tables[0].Rows[index];
current_word = drow.ItemArray.GetValue(0).ToString();
words.Add(current_word);
}
Console.WriteLine(query);
Console.WriteLine("array:" + words);
foreach (String key in words)
{
int len = key.Length;
string replace = "";
for ( index = 0; index < len; index++)
{
replace += "*";
}
html.Replace(key, replace);
//count++;
}
//Console.WriteLine("Total number of words: " + count);
doc2.body.innerHTML = html.ToString();
}
发布于 2018-09-30 07:26:02
一种方法是使用正则表达式进行计数和替换:
Dictionary<string,int> counts = new Dictionary<string, int>();
foreach (var word in words)
{
var matches = Regex.Matches(html, $@"\b{word}\b", RegexOptions.IgnoreCase);
counts.Add(word, matches.Count);
html = Regex.Replace(html, $@"\b{word}\b", "".PadLeft(word.Length, '*'), RegexOptions.IgnoreCase);
}
foreach (var kv in counts)
{
Console.WriteLine($"{kv.Key}:{kv.Value}");
}
Console.WriteLine(html);
发布于 2018-09-30 07:44:24
我希望这对你有帮助。
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace CountNumberTest
{
public class WebBrowserReplaceHtmlFromDatabaseTest
{
private const string BaseAddress = "https://stackoverflow.com/questions/52572969/count-number-of-occurrence";
private readonly ITestOutputHelper Output;
private static readonly HttpClient StaticHttpComponent = new HttpClient
{
BaseAddress = new Uri(BaseAddress)
};
public WebBrowserReplaceHtmlFromDatabaseTest(ITestOutputHelper output)
{
this.Output = output;
}
[Fact]
public async Task When_GetHtmlBodyFromWebPage_Then_ReplaceAllWordInHtml()
{
var replaceWordCollection = new[] { "html", "I", "in", "on" };
var request = new HttpRequestMessage(HttpMethod.Get, $"{BaseAddress}");
HttpResponseMessage response = await StaticHttpComponent.SendAsync(request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
StringBuilder buffer = new StringBuilder(await response.Content.ReadAsStringAsync());
Assert.NotEqual(0, buffer.Length);
foreach (string word in replaceWordCollection)
{
int len = word.Length;
int countOccurrence = Regex.Matches(buffer.ToString(), word).Count;
var replaceWord = new string('*', len);
Output.WriteLine($"{word} - occurred: {countOccurrence}");
buffer.Replace(word, replaceWord);
}
string html = buffer.ToString();
foreach (string word in replaceWordCollection)
Assert.DoesNotContain(word, html);
Output.WriteLine(html);
}
}
}
https://stackoverflow.com/questions/52572969
复制相似问题