【问题标题】:Get text separate by <br></br> inside TD tag using VBA使用VBA在TD标签内获取由<br></br>分隔的文本
【发布时间】:2020-04-23 19:15:47
【问题描述】:

我使用此代码获取 TD 标签

Set TDElement = oHtml.getElementsByTagName("tr")(6).getElementsByTagName("td")(18)

这会将 TD 标记的整个文本返回给我。

Text1
Text2
Text3

我想要做的是在&lt;br&gt;&lt;/br&gt;之前获取每个单独的字符串

这是 HTML 页面:

<td class="Td" nowrap="" valign="top">
  text1<br></br>
  text2<br></br>
  text3<br></br>
</td>

如何在 TD 标签内迭代这个字符串

【问题讨论】:


  • 替换为空,并使用SPLIT函数由分割,或者使用内部文本
  • 感谢@Nathan_Sav,这对我有用。
  • @Nathan_Sav:你为什么不发表你的评论作为答案?
  • 顺便说一句,&lt;br&gt; 标签是一个空标签,这意味着它没有结束标签。

标签: html vba excel web-scraping


【解决方案1】:

有点像....

    dim strSplit() as string
    dim intCounter as integer
    dim strSelection as string

    strHTML=replace(strHTML,"<br>","")
    strSplit=split(strHTML,"</br>")

    for intCounter=0 to ubound(strSplit)
        strSelection = strSplit(intCounter)
    next intCounter

【讨论】:

    【解决方案2】:

    br 不是 HTML 页面的元素,不能被指向。

    表示中断(换行)。

    所以对于你的例子,如果你得到这个td标签的innerText,比如:

    IE.document.getElementsByTagName("td")(**this td number**).innerText 
    

    你会得到:

    text1
    
    text2
    
    text3 (Note lines in between)
    

    您必须提取 td 标记的内部文本并使用字符串操作将换行符作为分隔符拆分为 3 个字符串。

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 2021-07-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      相关资源
      最近更新 更多