C# 解析HTML 四种方式

  1. WebClient
    下载webpage 到本地或string中
System.Net.WebClient client = new WebClient();
            byte[] page = client.DownloadData("http://www.google.com");
            string content = System.Text.Encoding.UTF8.GetString(page);
            string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
            Regex re = new Regex(regex);
            MatchCollection matches = re.Matches(content);

            System.Collections.IEnumerator enu = matches.GetEnumerator();
            while (enu.MoveNext() && enu.Current != null)
            {
                Match match = (Match)(enu.Current);
                Console.Write(match.Value + "\r\n");
            }

根据正则表达式分析,上面是解析 href 的案例。


  1. Winista.Htmlparser.Net
    获取HTML 树结构
try
    {

        txtHtmlWhole.Text = "";
        string url = CBUrl.SelectedItem.ToString().Trim();
        System.Net.WebClient aWebClient = new System.Net.WebClient();
        aWebClient.Encoding = System.Text.Encoding.Default;
        string html = aWebClient.DownloadString(url);
        txtHtmlWhole.Text = html;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    #endregion

    #region 分析网页html节点
    Lexer lexer = new Lexer(this.txtHtmlWhole.Text);
    Parser parser = new Parser(lexer);
    NodeList htmlNodes = parser.Parse(null);
    

  1. HtmlAgilityPack
//初始化网络请求客户端
HtmlWeb webClient = new HtmlWeb();
//初始化文档
HtmlDocument doc = webClient.Load("http://www.cnblogs.com/");
//查找节点
HtmlNodeCollection titleNodes = doc.DocumentNode.SelectNodes("//a[@class='titlelnk']");
if (titleNodes != null)
{
    foreach (var item in titleNodes)
    {
        Console.WriteLine(item.InnerText);
    }
}
Console.Read();

  1. SgmlReader
    用这个工具先将html文件转成标准的xml格式文件,再通过制定xpath路径来提取所需要的内容(xpath路径可以通过上面的那个工具生成)。
XPathDocument pathDoc = null;
using (SgmlReader sgmlReader = new SgmlReader())
{
    sgmlReader.DocType = "HTML";
    sgmlReader.InputStream = new StringReader(html);
    using (StringWriter stringWriter = new StringWriter())
    {
        using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter))
        {
            while (!sgmlReader.EOF)
            {
                xmlWriter.WriteNode(sgmlReader, true);
            }
            string xml = stringWriter.ToString().Replace("xmlns=\"http://www.w3.org/1999/xhtml\"", "");
            pathDoc = new XPathDocument(new StringReader(xml));
        }                    
    }
}
//提取出整个table
string xpath = "//div[@class=\"infoList\"]/table";//xpath表达式
XPathNavigator nav = pathDoc.CreateNavigator();
XPathNodeIterator nodes = nav.Select(xpath);
if (!nodes.MoveNext())
{
    return;
}
nodes = nodes.Current.Select("//tr");
if (!nodes.MoveNext()) return;
string str = "";
while (nodes.MoveNext())
{
    //遍历所有行
    XPathNodeIterator tdNode = nodes.Current.Select("./td");
    while (tdNode.MoveNext())
    {
        //遍历列
        str += tdNode.Current.Value.Trim() + " ";
    }
    str += "\r\n";  
}
//输出结果
Console.WriteLine(str);
  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风干牛肉巴旦木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值