Http敏捷包xpath

时间:2012-04-30 07:07:57

标签: vb.net xpath html-agility-pack

我有一个问题,我无法正常使用HTTP AGILITY包PACK,例如,想要检索“样式”中包含的图像的地址,我想知道是否有人建议我使用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>

代码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

编辑:代码缩进改进

1 个答案:

答案 0 :(得分: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

我希望它可以帮到你