【问题标题】:Reading Numbers from Webbrowser Vb.net从 Webbrowser Vb.net 读取数字
【发布时间】:2023-04-04 00:04:01
【问题描述】:
在我的网络浏览器中,我收到如下文本:剩余余额:10 美元
我想将其转换为另一种货币,我只想从浏览器中读取数字,然后将其发送到带有新转换货币的标签或文本框。我被困在这里了。
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
【问题讨论】:
标签:
vb.net
webbrowser-control
currency
【解决方案1】:
我有这个建议(我知道这可能不是完美的解决方案):
- 首先,尝试在普通网络浏览器(例如 Google Chrome 或 Firefox 或任何具有“检查元素”功能(即查看其 HTML 代码)的浏览器)中加载相同的网页。
- 找出显示您想要的价格的元素。
- 记下元素的 ID(通常写成
id="someID" 之类的东西)
-
回到您的程序代码,包括以下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
- 可能在加载
WebBrowser2 的文档时调用Function,即在WebBrowser2_DocumentCompleted 子目录中。
希望对你有帮助!