C# 使用asp.net C在网站中查找特定单词#
如何使用asp.net C#在网站中查找特定单词。 例如,如果我在浏览cnn.com网站时想在网站上浏览一个单词,如sport,我如何使用asp.net C#找到它C# 使用asp.net C在网站中查找特定单词#,c#,asp.net,C#,Asp.net,如何使用asp.net C#在网站中查找特定单词。 例如,如果我在浏览cnn.com网站时想在网站上浏览一个单词,如sport,我如何使用asp.net C#找到它 谢谢您可以用c#构建一种“爬虫”,捕获主页,然后递归页面上的链接。爬虫将为每个页面下载html源代码,您可以进行简单的文本搜索。描述它感觉很野蛮,但它可以工作。你可以用c语言构建一种“爬虫程序”,捕捉主页,然后递归页面上的链接。爬虫将为每个页面下载html源代码,您可以进行简单的文本搜索。描述它感觉很野蛮,但它可以工作。使用此代码可
谢谢您可以用c#构建一种“爬虫”,捕获主页,然后递归页面上的链接。爬虫将为每个页面下载html源代码,您可以进行简单的文本搜索。描述它感觉很野蛮,但它可以工作。你可以用c语言构建一种“爬虫程序”,捕捉主页,然后递归页面上的链接。爬虫将为每个页面下载html源代码,您可以进行简单的文本搜索。描述它感觉很野蛮,但它可以工作。使用此代码可以将网页作为字符串
string webpageData;
using (System.Net.WebClient webClient = new System.Net.WebClient())
webpageData = webClient.DownloadString("http://www.cnn.com");
然后只使用常规字符串方法
var containsWord = webpageData.Contains("word");
您可以使用此代码以字符串形式获取网页
string webpageData;
using (System.Net.WebClient webClient = new System.Net.WebClient())
webpageData = webClient.DownloadString("http://www.cnn.com");
然后只使用常规字符串方法
var containsWord = webpageData.Contains("word");
如果我正确理解了您的问题,您希望能够以编程方式浏览网站并找到给定单词的位置。为了做到这一点,您可以使用WebClient类来加载页面的html内容,然后使用正则表达式来匹配所需的单词。下面是一个加载cnn.com并列出此网站上所有链接及其位置的示例,您可以修改regualr表达式以仅返回包含单词sport的链接
WebClient client = new WebClient();
using (Stream data = client.OpenRead(@"http://www.cnn.com/"))
{
using (StreamReader reader = new StreamReader(data))
{
string content = reader.ReadToEnd();
string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)";
MatchCollection matches = Regex.Matches(content, pattern);
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
Console.WriteLine("'{0}' repeated at position {1}",
groups[0].Value, groups[0].Index);
}
}
}
如果我正确理解了您的问题,您希望能够以编程方式浏览网站并找到给定单词的位置。为了做到这一点,您可以使用WebClient类来加载页面的html内容,然后使用正则表达式来匹配所需的单词。下面是一个加载cnn.com并列出此网站上所有链接及其位置的示例,您可以修改regualr表达式以仅返回包含单词sport的链接
WebClient client = new WebClient();
using (Stream data = client.OpenRead(@"http://www.cnn.com/"))
{
using (StreamReader reader = new StreamReader(data))
{
string content = reader.ReadToEnd();
string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)";
MatchCollection matches = Regex.Matches(content, pattern);
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
Console.WriteLine("'{0}' repeated at position {1}",
groups[0].Value, groups[0].Index);
}
}
}
了解您在页面中搜索单词的原因可能会有所帮助。i、 是为了提取一部分数据,还是仅仅为了建立自己的索引服务?了解您在页面中搜索单词的原因可能会有所帮助。i、 是为了提取一部分数据,还是仅仅为了构建自己的索引服务?