2012-12-08 71 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

相關問題