使用asp.net C#发现一个网站的特定单词 [英] find a specific word in a website using asp.net C#

查看:77
本文介绍了使用asp.net C#发现一个网站的特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能找到在使用asp.net C#网站的特定词。
例如,如果我在浏览cnn.com站点,我想浏览一个字,如在网站上运动,怎么能找到我使用asp.net C#吧。

How can I find a specific word in a website using asp.net C#. example, if I in browse cnn.com website and I want to browse a word such as sport in the website, how can i find it using the asp.net C#.

感谢

推荐答案

如果我理解正确你的问题,你希望能够以编程方式浏览网站,并找到给出的单词位置。为了做到这一点,你可以以加载网页的HTML内容,那么正则表达式匹配需要的话使用WebClient类。下面是这将加载cnn.com并列出所有本网站上找到的链接和它们的位置一个例子,你可以修改regualr前pression仅返回包含单词运动链接

if I understood your question correctly you want to be able to programmatically browse a website and find positions of given words. In order to do this you can use WebClient class in order to load the html content of the page then Regex to match needed words. Below is an example which would load cnn.com and list all the links found on this website and their positions, you can modify the regualr expression to return only links which contain word 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);
        }
    }
}

这篇关于使用asp.net C#发现一个网站的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆