我想用HtmlAgilityPack檢索html元素的寬度和高度


如何使用HtmlAgilityPack檢索img元素的寬度和高度我這樣做..

    HtmlAgilityPack.HtmlAttribute width = link.Attributes["width"];
    HtmlAgilityPack.HtmlAttribute height = link.Attributes["height"];

但寬度和高度在大多數情況下是null。如何獲得css的高度和寬度?

熱門答案

該頁面中的Basead: page

public sealed class UtilParserHTML
{

    //Private Fields
    private Uri Uri;
    private Stream StreamPage;
    private HttpWebRequest HttpRequest;
    private HttpWebResponse HttpResponse;

    //Public Fields
    public HtmlDocument HtmlDocument { private set; get; }

    public UtilParserHTML()
    {
        if (this.HtmlDocument == null)
            HtmlDocument = new HtmlDocument();
    }

    public void LoadHTMLPage(string UrlPage)
    {
        if (string.IsNullOrEmpty(UrlPage))
            throw new ArgumentNullException("");

        CookieContainer cookieContainer = new CookieContainer();

        this.Uri = new Uri(UrlPage);
        this.HttpRequest = (HttpWebRequest)WebRequest.Create(UrlPage);
        this.HttpRequest.Method = WebRequestMethods.Http.Get;       
        this.HttpRequest.CookieContainer = cookieContainer;
        this.HttpResponse = (HttpWebResponse)this.HttpRequest.GetResponse();
        this.StreamPage = this.HttpResponse.GetResponseStream();
        this.HtmlDocument.Load(StreamPage);
    }

    public void LoadHTMLPage(FileStream StreamPage)
    {
        if (StreamPage == null)
            throw new ArgumentNullException("");

        HtmlDocument.Load(StreamPage);
    }

    public HtmlNodeCollection GetNodesByExpression(string XPathExpression)
    {
        if (string.IsNullOrEmpty(XPathExpression))
            throw new ArgumentNullException("");

        return this.HtmlDocument.DocumentNode.SelectNodes(XPathExpression);
    }

使用XPath在html中導航。在這種情況下,我使用了這個Xpath表達式:// div [@ class ='arrowRibbon'] // img

看: ......

public sealed class UtilParserHTML
{

    //Private Fields
    private Uri Uri;
    private Stream StreamPage;
    private HttpWebRequest HttpRequest;
    private HttpWebResponse HttpResponse;

    //Public Fields
    public HtmlDocument HtmlDocument { private set; get; }

    public UtilParserHTML()
    {
        if (this.HtmlDocument == null)
            HtmlDocument = new HtmlDocument();
    }

    public void LoadHTMLPage(string UrlPage)
    {
        if (string.IsNullOrEmpty(UrlPage))
            throw new ArgumentNullException("");

        CookieContainer cookieContainer = new CookieContainer();

        this.Uri = new Uri(UrlPage);
        this.HttpRequest = (HttpWebRequest)WebRequest.Create(UrlPage);
        this.HttpRequest.Method = WebRequestMethods.Http.Get;       
        this.HttpRequest.CookieContainer = cookieContainer;
        this.HttpResponse = (HttpWebResponse)this.HttpRequest.GetResponse();
        this.StreamPage = this.HttpResponse.GetResponseStream();
        this.HtmlDocument.Load(StreamPage);
    }

    public void LoadHTMLPage(FileStream StreamPage)
    {
        if (StreamPage == null)
            throw new ArgumentNullException("");

        HtmlDocument.Load(StreamPage);
    }

    public HtmlNodeCollection GetNodesByExpression(string XPathExpression)
    {
        if (string.IsNullOrEmpty(XPathExpression))
            throw new ArgumentNullException("");

        return this.HtmlDocument.DocumentNode.SelectNodes(XPathExpression);
    }

...

編輯

查看寬度和高度的完整示例:

public sealed class UtilParserHTML
{

    //Private Fields
    private Uri Uri;
    private Stream StreamPage;
    private HttpWebRequest HttpRequest;
    private HttpWebResponse HttpResponse;

    //Public Fields
    public HtmlDocument HtmlDocument { private set; get; }

    public UtilParserHTML()
    {
        if (this.HtmlDocument == null)
            HtmlDocument = new HtmlDocument();
    }

    public void LoadHTMLPage(string UrlPage)
    {
        if (string.IsNullOrEmpty(UrlPage))
            throw new ArgumentNullException("");

        CookieContainer cookieContainer = new CookieContainer();

        this.Uri = new Uri(UrlPage);
        this.HttpRequest = (HttpWebRequest)WebRequest.Create(UrlPage);
        this.HttpRequest.Method = WebRequestMethods.Http.Get;       
        this.HttpRequest.CookieContainer = cookieContainer;
        this.HttpResponse = (HttpWebResponse)this.HttpRequest.GetResponse();
        this.StreamPage = this.HttpResponse.GetResponseStream();
        this.HtmlDocument.Load(StreamPage);
    }

    public void LoadHTMLPage(FileStream StreamPage)
    {
        if (StreamPage == null)
            throw new ArgumentNullException("");

        HtmlDocument.Load(StreamPage);
    }

    public HtmlNodeCollection GetNodesByExpression(string XPathExpression)
    {
        if (string.IsNullOrEmpty(XPathExpression))
            throw new ArgumentNullException("");

        return this.HtmlDocument.DocumentNode.SelectNodes(XPathExpression);
    }

希望這有幫助。





許可下: CC-BY-SA
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因