【问题标题】:HTMLAgilityPack getting <P> and <STRONG> textHTMLAgilityPack 获取 <P> 和 <STRONG> 文本
【发布时间】:2012-10-27 03:09:56
【问题描述】:

大家好,我正在寻找获取此 HTML 代码的方法:

<DIV class=schedule_block>
<DIV class=channel_row><SPAN class=channel>
<DIV class=logo><IMG src='/images/channel_logos/WGNAMER.png'></DIV>
<P><STRONG>2</STRONG><BR>WGNAMER </P></SPAN>

使用 HtmlAgilityPack。

我一直在尝试这个:

For Each channel In doc.DocumentNode.SelectNodes(".//div[@class='channel_row']")
   Dim info = New Dictionary(Of String, Object)()

   With channel
      info!Logo = .SelectSingleNode(".//img").Attributes("src").Value
      info!Channel = .SelectSingleNode(".//span[@class='channel']").ChildNodes(1).ChildNodes(0).InnerText
      info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(1).ChildNodes(2).InnerText
   End With
.......

我可以获得徽标,但它为频道和电台提供了一个空白字符串

索引超出范围。必须是非负数且小于 集合。

我尝试了所有类型的组合:

info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(1).ChildNodes(1).InnerText
info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(1).ChildNodes(3).InnerText
info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(0).ChildNodes(1).InnerText
info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(0).ChildNodes(2).InnerText
info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(0).ChildNodes(3).InnerText

我需要做什么来纠正这个问题?

【问题讨论】:

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


    【解决方案1】:

    如果空格确实存在,则将其视为子节点。所以:

    Dim channelSpan = .SelectSingleNode(".//span[@class='channel']")
    
    info!Channel = channelSpan.ChildNodes(3).ChildNodes(0).InnerText
    info!Station = channelSpan.ChildNodes(3).ChildNodes(2).InnerText

    【讨论】:

    • 索引超出范围当我这样做时,现在频道的索引超出了范围。
    • @StealthRT:channelSpan.ChildNodes.Count 是什么?
    • 我没有看到“C”中列出的内容?再次检查 OP。
    • @StealthRT:展开 ChildNodes 并查找 Count 属性。
    • 里面有4个...更新OP
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    相关资源
    最近更新 更多