C#解析HTML源码

刚做了一个小任务,需要抓取其他网站的部分数据,这里就顺便介绍使用Winista.Text.HtmlParser这个类库如何解析HTML并抓取部分数据

1、获取指定网站的页面源码

string url = "http://www.100njz.com/price/list/p--------1.html";
System.Net.WebClient aWebClient = new System.Net.WebClient();
aWebClient.Encoding = System.Text.Encoding.Default;
string html = aWebClient.DownloadString(url);

2、获取到源码后,解析并获取指定的节点数据,这里演示的是获取id="articleList"的div

Lexer lexer = new Lexer(html);
        Parser parser = new Parser(lexer);
        NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.Div));
        NodeList nodeList = parser.Parse(filter);
        ITag t;
        if (nodeList.Count == 0)
            Response.Write("没有符合要求的节点"); else { for (int i = 0; i < nodeList.Count; i++) { t = getTag(nodeList[i]); if (t != null && t.GetAttribute("id") == "articleList") { NodeFilter filter2 = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.LinkTag)); Response.Write(nodeList[i].ToHtml()); } } }
private ITag getTag(INode node)
    {
        if (node == null)
            return null;
        return node is Div ? node as Div : null;
    }
 
  

 

 
 

 

转载于:https://www.cnblogs.com/KingUp/p/4328962.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值