0

我有一个问题,我不能正确使用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

编辑:代码缩进改进

4

1 回答 1

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

希望对你有帮助

于 2012-05-03T10:31:57.343 回答