我想使用HtmlAgilityPack检索html元素的宽度和高度 [英] I want to retrieve the width and height of html element with HtmlAgilityPack

查看:99
本文介绍了我想使用HtmlAgilityPack检索html元素的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用HtmlAgilityPack检索img元素的宽度和高度,我是这样的.

How to retrieve the width and height of img element with HtmlAgilityPack I do this like this..

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

,但宽度和高度在大多数情况下为null.如何获得CSS的高度和宽度?

but the width and height is in most cases null. How to get the css height and width?

推荐答案

此页面的基础:页面

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

Use XPath to navigate in html. In this case I used this Xpath expression : //div[@class='arrowRibbon'] //img

看: ...

this.ParserHTML.LoadHTMLPage("http://www.img.com.br/default.aspx");
            HtmlNodeCollection HtmlNodeCollectionResult = this.ParserHTML.GetNodesByExpression(Page.XPathExpression);

            if (HtmlNodeCollectionResult != null)
            {
                foreach (HtmlNode NodeResult in HtmlNodeCollectionResult)
                {
                    var src = NodeResult.Attributes["src"].Value;
                }
            }

...

编辑

使用宽度和高度查看此完整示例:

Look this complete example with width and height:

this.ParserHTML.LoadHTMLPage("http://www.w3schools.com/tags/tag_img.asp");
            HtmlNodeCollection HtmlNodeCollectionResult = this.ParserHTML.GetNodesByExpression("//div[@class='tryit_ex'] //img");

            if (HtmlNodeCollectionResult != null)
            {
                foreach (HtmlNode NodeResult in HtmlNodeCollectionResult)
                {
                    var src = NodeResult.Attributes["src"].Value;
                    var w = NodeResult.Attributes["width"].Value;
                    var h = NodeResult.Attributes["height"].Value;
                }
            }

希望获得帮助.

这篇关于我想使用HtmlAgilityPack检索html元素的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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