2017-06-12 42 views
1

在兩個地點之間建立方向後,是否可以從HTML代碼中提取里程?獲取路線後,我可以在IE的頁面源代碼中看到里程數,並且我試圖通過使用下面的內容來提取它顯示的里程數。這可能很容易,但我很難過。在WebBrowser控件中使用Bing地圖在兩個地點之間獲取里程

有一點信息。我能夠通過WebBrowser控件的位置來獲取方向,並在IE中顯示方向。我想在文本框中獲得里程數。

HTML代碼

enter image description here

VB.NET代碼

Dim miles As String = "" 
Dim minutes As String = "" 
Dim strHtml As String = "" 
strHtml = wbMaps.DocumentText 

For i As Integer = 0 To strHtml.Length - 1 
    If InStr(strHtml(i).ToString, "descriptionDistance") Then 
     Dim bracketLoc As Integer = 0 
     Dim closeBracketLoc As Integer = 0 
     bracketLoc = InStr(strHtml(i).ToString, ">") 
     closeBracketLoc = InStr(strHtml(i).ToString, "<") 
     miles = Mid(strHtml(i).ToString, bracketLoc + 1, closeBracketLoc - 1) 
     haulMiles = Val(miles) 
     tb_Miles.Text = haulMiles.ToString 
    End If 
Next 

回答

0

我看你已經回答了你自己的問題,但我會添加另一個答案,如果你有興趣。而不是寫你自己的分析邏輯,你可以得到英里容易得多,如果你使用,你可以通過的NuGet添加到您的項目中HtmlAgilityPack:https://www.nuget.org/packages/HtmlAgilityPack/

 Dim htmlDoc As New HtmlAgilityPack.HtmlDocument 
     htmlDoc.LoadHtml(wbMaps.Document.Body.InnerHtml) 
     miles = htmlDoc.DocumentNode.SelectSingleNode("//td[@data-tag='descriptionDistance']").InnerHtml 
+0

非常感謝該信息。我會在今晚安排它並與之一起玩,以防我想獲得更復雜的信息。按照我上面描述的方式進行操作 - 我可能不太願意在2個不同的關鍵字之間提取內容。 –

1

我想通了。我用這個:

Dim miles As String = "" 
    Dim minutes As String = "" 
    Dim strHtml As StringBuilder 

    strHtml = New StringBuilder 

    strHtml.Append(wbMaps.DocumentText) 

    Dim lines() As String = Split(strHtml.ToString, vbCrLf) 


    For Each line As String In lines 

     If InStr(line, "descriptionDistance") Then 

      Dim bracketLoc As Integer = 0 
      Dim closeBracketLoc As Integer = 0 
      bracketLoc = InStr(line, ">") 
      closeBracketLoc = InStr(line, "<") 
      miles = Mid(line, bracketLoc + 1, closeBracketLoc - 1) 
      haulMiles = Val(miles) 
      tb_Miles.Text = miles 

     End If 
    Next 
相關問題