Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

c# - I want to retrieve the width and height of html element with HtmlAgilityPack

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"];

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basead in this page: 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);
    }

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

Look: ...

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;
                }
            }

...

EDIT

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;
                }
            }

Hope this help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...