从Webbrowser Vb.net读取数字

时间:2017-03-24 20:33:29

标签: vb.net webbrowser-control currency

在我的WebBrowser中,我得到了这样的文字:剩余余额:10美元
我想将它转换为另一种货币,我只想从我的浏览器中读取数字,然后我将它发送到带有新转换货币的Label或TextBox。我被困在这里。
A screenshot of it

Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
    WebBrowser2.Navigate(TextBox9.Text + TextBox2.Text)
End Sub
Private Sub WebBrowser2_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
    Label1.Text = (WebBrowser2.Document.Body.InnerText)
End Sub

1 个答案:

答案 0 :(得分:0)


我有这个建议(我知道这可能不是完美的解决方案):

  1. 首先,尝试在普通网络浏览器(例如Google Chrome或Firefox)或任何具有“检查元素”功能的浏览器中加载相同的网页,这意味着要查看其HTML代码。
  2. 找出显示您想要的价格的元素。
  3. 记下元素的ID(通常写成id="someID"
  4. 返回程序代码,包含以下Function,它将显示文本并将其转换为其他货币:

    Public Function ConvertDisplayedCurrency() As Decimal
        Dim MyCurrencyElement As HtmlElement = WebBrowser2.Document.GetElementById("theIdYouGotInStep3") 'This will refer to the element you want.
        Dim TheTextDisplayed As String = MyCurrencyElement.InnerText 'This will refer to the text that is displayed.
        'Assuming that the text begins like "Remaining balance: ###", you need to strip off that first part, which is "Remaining balance: ".
        Dim TheNumberDisplayed As String = TheTextDisplayed.Substring(19)
        'The final variable TheNumberDisplayed will be resulting into a String like only the number.
        Dim ParsedNumber As Decimal = 0 'A variable which will be used below.
        Dim ParseSucceeded As Boolean = Decimal.TryParse(TheNumberDisplayed, ParsedNumber)
        'The statement above will TRY converting the String TheNumberDisplayed to a Decimal.
        'If it succeeds, the number will be set to the variable ParsedNumber and the variable
        'ParseSucceeded will be True. If the conversion fails, the ParseSucceeded will be set
        'to False.
        If Not ParseSucceeded = True Then Return 0 : Exit Function 'This will return 0 and quit the Function if the parse was a failure.
    
        'Now here comes your turn. Write your own statements to convert the number "ParsedNumber"
        'to your new currency and finally write "Return MyFinalVariableName" in the end.
    
    End Function
    
  5. Function的文档加载时,WebBrowser2加载,WebBrowser2_DocumentCompleted
  6. 我希望它有所帮助!