Vb 2010 / VS 2012 | - 如何从网站元素ID获取内部文本? [英] Vb 2010/VS 2012| - how to get the inner text from a website element id?

查看:47
本文介绍了Vb 2010 / VS 2012 | - 如何从网站元素ID获取内部文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从网站上的元素获取文字



网站元素代码:



 <   div     class   = 信息框 >  
< span class = info-box-icon bg-yellow >

< / span >
< div class = info-box-content >
< span class = info-box-text > ; 总成员< / span > ;
< span class = info-box-number > 34,396 < / span >
< / div >
< / div >





我试图从信息箱号码中获得34,396。



这些箱子中有4个是我需要的。它们都是相同的,除了Info-Box-Text和Info-Box-Number是不同的。



我尝试了什么:



我试图使用GetAttribute,但我一直收到错误。



 私有  Sub  Form1_Load(发件人作为 对象,e 作为 EventArgs)句柄 < span class =code-keyword> MyBase  .Load 
Textbox1.Text = WebBrowser1.Document.GetElementsByTagName( info-text-box)。GetAttribute( info-text-number )。ToString()
结束 Sub





获得号码后,我希望它进入TextBox。



-UPDATE -

尝试

 TextBox1.Text = WebBrowser1.Document.GetElementById(  info-box-number) .InnerText 





我得到错误:

MyTool中出现未处理的'System.NullReferenceException'类型异常.exe



附加信息:对象引用未设置为对象的实例。

解决方案

使用WebBrowser的

 Event DocumentCompleted()

,而不是Form Load()事件。



< pre lang =vb> 私有 Sub WebBrowser1_DocumentCompleted(sender 正如 对象,e As WebBrowserDocumentCompletedEventArgs) Handles WebBr owser1.DocumentCompleted

TextBox1.Text = WebBrowser1.Document.GetElementById( infobox-number )。InnerText
MsgBox( DOCUMENT_COMPLETED
< span class =code-keyword>结束 Sub





...但我注意到DocumentCompleted()事件已经引发了数十次,当我向上和向下滚动时。

这里我给出了一些我多年前在我的项目中使用的代码。它不使用WebBrowser,而是直接下载数据。尝试根据您的需要调整它。



 私人 函数 GetInfo()作为 字符串 
< span class =code-keyword> dim body as string =
尝试
< span class =code-keyword> Dim ur As Uri( http://www.find-ip-address.org/
Dim req 作为 HttpWebRequest = CType (HttpWebRequest.Create(ur ),HttpWebRequest)
Dim res As HttpWebResponse = CType (req.GetResponse(),HttpWebResponse)
如果(res.StatusCode = HttpStatusCode.OK)那么
Dim receiveStream < span class =code-keyword> As Stream = res.GetResponseStream()
Dim readStream 作为 StreamReader = Nothing
如果(res.CharacterSet = Nothing 然后
readStream = StreamReader (receiveStream)
Else
readStream = StreamReader(receiveStream,Encoding.GetEncodin g(res.CharacterSet))
Dim data As String = readStream.ReadToEnd()
' 数据包含所有HTML文本
' #################### ############
res.Close()
readStream.Close()

Dim left = 我的IP国家名称:
Dim right = IP地址查找位置
Dim indexLeft 作为 整数 = data.IndexOf(L eft)
Dim indexRight 作为 整数 = data.IndexOf(右)
body = data.Substring(indexLeft + left.Length,indexRight - indexLeft - left.Length)
End 如果
结束 如果
结束 尝试
返回 body
结束 功能


I am attempting to get text from a Element on a website

Websites Element Code:

<div class="info-box">
<span class="info-box-icon bg-yellow">

</span>
<div class="info-box-content">
<span class="info-box-text">TOTAL MEMBERS</span>
<span class="info-box-number">34,396</span>
</div>
</div>



I am attempting to get 34,396 from info-box-number.

There are 4 of these boxes that I need to get them from. They are all the same except the Info-Box-Text and Info-Box-Number are different.

What I have tried:

I attempted to use GetAttribute but, I kept getting a error.

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   Textbox1.Text = WebBrowser1.Document.GetElementsByTagName("info-text-box").GetAttribute("info-text-number").ToString()
End Sub



After I get the number I want it to go into a TextBox.

-UPDATE-
Attempted

TextBox1.Text = WebBrowser1.Document.GetElementById("info-box-number").InnerText 



Error I got:
An unhandled exception of type 'System.NullReferenceException' occurred in MyTool.exe

Additional information: Object reference not set to an instance of an object.

解决方案

Use the

Event DocumentCompleted()

of the WebBrowser, instead of the Form Load() Event.

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        
  TextBox1.Text = WebBrowser1.Document.GetElementById("infobox-number").InnerText
  MsgBox("DOCUMENT_COMPLETED")
End Sub



... but i noticed that the DocumentCompleted() Event raised dozens of times, also when i scrolled up&down.
Here i give some code that i used years ago on my project. It does not use the WebBrowser, instead it downloads the data directly. Try to tweak it for your needs.

Private Function GetInfo() As String
dim body as string = ""
        Try 
            Dim ur As New Uri("http://www.find-ip-address.org/")
            Dim req As HttpWebRequest = CType(HttpWebRequest.Create(ur), HttpWebRequest)
            Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
            If (res.StatusCode = HttpStatusCode.OK) Then
                Dim receiveStream As Stream = res.GetResponseStream()
                Dim readStream As StreamReader = Nothing
                If (res.CharacterSet = Nothing) Then
                    readStream = New StreamReader(receiveStream)
                Else
                    readStream = New StreamReader(receiveStream, Encoding.GetEncoding(res.CharacterSet))
                    Dim data As String = readStream.ReadToEnd()
                    'data contains all HTML text
'################################
                    res.Close()
                    readStream.Close()

                    Dim left = "My IP Country Name:"
                    Dim right = "IP Address Lookup Location"
                    Dim indexLeft As Integer = data.IndexOf(left)
                    Dim indexRight As Integer = data.IndexOf(right)
                    body = data.Substring(indexLeft + left.Length, indexRight - indexLeft - left.Length)
                End If
            End If
        End Try
            Return body
End Function


这篇关于Vb 2010 / VS 2012 | - 如何从网站元素ID获取内部文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆