如何使用HTML Agility包

619 html c# html-agility-pack

我如何使用HTML Agility Pack

我的XHTML文档并不完全有效.这就是我想要使用它的原因.我如何在我的项目中使用它?我的项目是在C#中.

Ash*_*Ash 357

首先,将HTMLAgilityPack nuget包安装到项目中.

然后,作为一个例子:

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

// There are various options, set as needed
htmlDoc.OptionFixNestedTags=true;

// filePath is a path to a file containing the html
htmlDoc.Load(filePath);

// Use:  htmlDoc.LoadHtml(xmlString);  to load from a string (was htmlDoc.LoadXML(xmlString)

// ParseErrors is an ArrayList containing any errors from the Load statement
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
{
    // Handle any parse errors as required

}
else
{

    if (htmlDoc.DocumentNode != null)
    {
        HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");

        if (bodyNode != null)
        {
            // Do something with bodyNode
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

(注意:此代码仅为示例,不一定是最佳/唯一的方法.请勿在您自己的应用程序中盲目使用它.)

HtmlDocument.Load()方法还接受一个流,该流在与.NET框架中的其他面向流的类集成时非常有用.虽然HtmlEntity.DeEntitize()是另一种正确处理html实体的有用方法.(感谢马修)

HtmlDocument并且HtmlNode 是您最常使用的课程.与XML解析器类似,它提供了接受XPath表达式的selectSingleNode和selectNodes方法.

注意HtmlDocument.Option?????? 布尔属性.这些控制LoadLoadXML方法将如何处理您的HTML/XHTML.

还有一个名为HtmlAgilityPack.chm的编译帮助文件,其中包含每个对象的完整参考.这通常位于解决方案的基本文件夹中.

  • 另请注意,Load接受Stream参数,这在许多情况下都很方便.我用它作为HTTP流(WebResponse.GetResponseStream).另一个需要注意的好方法是HtmlEntity.DeEntitize(HTML Agility Pack的一部分).在某些情况下,需要手动处理实体. (11认同)
  • 不,SelectSingleNode和SelectNodes肯定还在那里.我发现它有点有趣,它应该是htmlDoc.ParseErrors.Count(),而不是.Count (3认同)

rtp*_*rry 166

我不知道这对你有什么帮助,但我写了几篇介绍基础知识的文章.

下一篇文章完成了95%,我只需要写下我编写的代码的最后几部分的解释.如果您有兴趣,我会尽量记得在发布时发布.

  • 最后两年后完成那篇文章:) [用HtmlAgilityPack检测网站中RSS和Atom提要的直接方法](http://runtingsproper.blogspot.co.uk/2012/07/a-straightforward-method-to-detecting的.html) (16认同)
  • 最近在_Code Project_中发布了一篇非常好的HTMLAgilityPack文章.你可以在这里阅读[http://www.codeproject.com/Articles/691119/Html-Agility-Pack-Massive-information-extraction-f) (3认同)

Ken*_*sen 64

HtmlAgilityPack使用XPath语法,虽然许多人认为它的文档很少,但在使用XPath文档的帮助下我可以毫不费力地使用它:https://www.w3schools.com/xml/xpath_syntax.asp

要解析

<h2>
  <a href="">Jack</a>
</h2>
<ul>
  <li class="tel">
    <a href="">81 75 53 60</a>
  </li>
</ul>
<h2>
  <a href="">Roy</a>
</h2>
<ul>
  <li class="tel">
    <a href="">44 52 16 87</a>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我这样做了:

string url = "http://website.com";
var Webget = new HtmlWeb();
var doc = Webget.Load(url);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//h2//a"))
{
  names.Add(node.ChildNodes[0].InnerHtml);
}
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//li[@class='tel']//a"))
{
  phones.Add(node.ChildNodes[0].InnerHtml);
}
Run Code Online (Sandbox Code Playgroud)


cap*_*sac 6

主要的HTMLAgilityPack相关代码如下

using System;
using System.Net;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Text.RegularExpressions;
using HtmlAgilityPack;

namespace GetMetaData
{
    /// <summary>
    /// Summary description for MetaDataWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class MetaDataWebService: System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = false)]
        public MetaData GetMetaData(string url)
        {
            MetaData objMetaData = new MetaData();

            //Get Title
            WebClient client = new WebClient();
            string sourceUrl = client.DownloadString(url);

            objMetaData.PageTitle = Regex.Match(sourceUrl, @
            "\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

            //Method to get Meta Tags
            objMetaData.MetaDescription = GetMetaDescription(url);
            return objMetaData;
        }

        private string GetMetaDescription(string url)
        {
            string description = string.Empty;

            //Get Meta Tags
            var webGet = new HtmlWeb();
            var document = webGet.Load(url);
            var metaTags = document.DocumentNode.SelectNodes("//meta");

            if (metaTags != null)
            {
                foreach(var tag in metaTags)
                {
                    if (tag.Attributes["name"] != null && tag.Attributes["content"] != null && tag.Attributes["name"].Value.ToLower() == "description")
                    {
                        description = tag.Attributes["content"].Value;
                    }
                }
            } 
            else
            {
                description = string.Empty;
            }
            return description;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 该网站已不再可用 (4认同)

小智 5

    public string HtmlAgi(string url, string key)
    {

        var Webget = new HtmlWeb();
        var doc = Webget.Load(url);
        HtmlNode ourNode = doc.DocumentNode.SelectSingleNode(string.Format("//meta[@name='{0}']", key));

        if (ourNode != null)
        {


                return ourNode.GetAttributeValue("content", "");

        }
        else
        {
            return "not fount";
        }

    }
Run Code Online (Sandbox Code Playgroud)