从网络浏览器读取数字 Vb.net
Reading Numbers from Webbrowser Vb.net
在我的 WebBrowser 中我得到这样的文本:剩余余额: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
我有这个建议(我知道这可能不是完美的解决方案):
- 首先,尝试在 Google Chrome 或 Firefox 等普通网络浏览器或任何具有 "inspect elements" 功能的浏览器中加载相同的网页,意思是查看他们的 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
子文件中。
希望对您有所帮助!
在我的 WebBrowser 中我得到这样的文本:剩余余额: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
我有这个建议(我知道这可能不是完美的解决方案):
- 首先,尝试在 Google Chrome 或 Firefox 等普通网络浏览器或任何具有 "inspect elements" 功能的浏览器中加载相同的网页,意思是查看他们的 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
子文件中。
希望对您有所帮助!