2012-04-30 54 views
0

我有個問題,我無法正確使用HTTP AGILITY包PACK,例如,想要檢索「樣式」中包含的圖像的地址,我想知道是否有人建議我使用xpath。Http敏捷包xpath

代碼HTML

<TABLE id=uezszu_24 class="uiGrid fbPhotosGrid" cellSpacing=0 cellPadding=0> 
<TBODY> 
    <TR> 
    <TD class="vTop"> 
     <DIV class=Wrapper> 
     <A class="uiMediaThumb uiScrollableThumb uiMediaThumbHuge" href="www.cccc.com/index.php" 
     name=43563463 rel=theater aria-label="photo" ajaxify="dsgdgbdfgr45y6ghd"> 
     <I style="BACKGROUND-IMAGE: url(http://www.fressdgf.com/image.jpg)"></I> 
     </A> 
     </DIV> 
    </TD> 
    </TR> 
</TBODY> 
</TABLE> 

CODE VB

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 

    Dim site As HtmlAgilityPack.HtmlWeb = New HtmlWeb() 
    Dim document As HtmlAgilityPack.HtmlDocument = site.Load("https://www.site.com") 
    For Each table As HtmlNode In document.DocumentNode.SelectNodes("//tr") 

     ListBox1.Items.Add(table.InnerText) 

    Next 
End Sub 

編輯:代碼縮進提高

+0

你想要的樣式屬性的內容?或者風格屬性中的實際URI?如果是後者,則需要首先從節點獲取屬性。然後自己解析樣式塊。 HTML敏捷包不會爲你解析它,它只是看到了測試。正則表達式可能在這裏工作。 'background-image \ s *:\ s * url \ s * \(\ s *(? [^)] +))'可能會有所斬斷。 – jessehouwing

回答

0

我建議你使用Linq代替的XPath。如果你想獲得內部節點的背景圖片,你可以這樣做:

For Each tableRow As HtmlNode In document.DocumentNode.SelectNodes("//tr") 
    Dim italicNode As HtmlNode = tableRow.DescendantNodes().Where(Function(n) n.Name = "i") 
    Dim styleValue As String = italicNode.GetAttributeValue("style",String.Empty) 
    ListBox1.Items.Add(styleValue) 
Next 

我希望它能幫助你

+0

後代(「我」)甚至比.Where更好(Function(n)n.Name =「i」)。 – jessehouwing