2012-12-08 70 views
0

如何与HtmlAgilityPack检索img元素的宽度和高度,我这样做是这样的..我要检索的宽度和html元素的高度与HtmlAgilityPack

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

,但宽度和高度是最病例为空。如何获得CSS的高度和宽度?

+5

HTML!= CSS。 Html敏捷包解析HTML。它不会使用CSS处理等重建整个浏览器。 –

+0

那么,如何获取宽度和高度?任何解决方案 – user1575229

+0

请给我一些解决方案。我很困难。 – user1575229

回答

0

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 [@类= '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; 
       } 
      } 

...

EDIT

查找与宽度和高度这个完整的例子:

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

希望得到这个帮助。

+0

这似乎很漫长。我认为,如果我可以将Image类并将src分配给它的imageUrl属性,那么它应该能够让我的宽度和高度。你说什么? – user1575229

+0

Hi @ user1698985。是的,我编辑了这个例子。希望这个帮助。 – Gus

+0

实际上我正在做这个过程,它获取宽度,高度和宽度,如果它在html中定义的话。如果它在CSS中定义,那么就没有办法获得它。 – user1575229

相关问题