如何获取Div类的文本并放置在标签内?

时间:2017-12-05 03:07:53

标签: html vb.net class

我试图获得"勇气"这是在Poloniex的网站内部div,在完全加载网站后使用Timer每隔1秒放入一个标签内。

我的完全代码:

https://pastebin.com/QrcuWnxf

股利:

<div class="info">valor</div>

我发现页面上有几个&#34; info&#34;类...

inserir a descrição da imagem aqui

此代码有效,但它带来的结果超出预期...

    Dim theElementCollection As HtmlElementCollection
    theElementCollection = WebBrowser1.Document.GetElementsByTagName("Div")
    For Each curElement As HtmlElement In theElementCollection
        If curElement.OuterHtml.Contains("info") Then
            Variable1 = (curElement.GetAttribute("InnerText"))
        End If
    Next

Label1.Text = Variable1

结果是这个Div的值也有&#34; info&#34;类。

<div class="msg"><div class="info">OMG/BTC and OMG/ETH  OmiseGO markets added</div>

1 个答案:

答案 0 :(得分:1)

更改第二次尝试:

For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
  If Element.GetAttribute("className") = "lastPrice" Then
   For Each Element1 As HtmlElement In Element.GetElementsByTagName("div")
        If Element1.GetAttribute("className") = "info" Then
            Dim Variable1 as String = Element1.InnerText
        End If
    Next
  End If
Next

是的,您必须将className用于class属性。

此代码已根据以下HTML进行验证:此信息类别也超过1个。

<html>
 <body>
  <div class="firstPrice">
   <div class="name">First Price</div>
   <div class="info">11650.00</div>
  </div>
  <div class="lastPrice">
   <div class="name">Last Price</div>
   <div class="info">11650.00</div>
  </div>
 </body>
</html>