2013-08-03 51 views
2

剛開始使用html,在vba中合理的能力,但在連接兩者時遇到了一些問題。使用vba從網上檢索數據

我已將註冊通過網站並嘗試獲得結果。 代碼中使用至今

Dim HTMLDoc As HTMLDocument 
Dim MyBrowser As InternetExplorer 
    Sub GetVehicleDetails() 

    Dim MyHTML_Element As IHTMLElement 
    Dim MyURL As String 
    Dim x As Integer 
    On Error GoTo Err_Clear 
    MyURL = "http://www.1stchoice.co.uk/find-a-part" 
    x = 0 
    Set MyBrowser = New InternetExplorer 
    MyBrowser.Silent = True 
    MyBrowser.navigate MyURL 
    MyBrowser.Visible = True 
    Do 
    Loop Until MyBrowser.readyState = READYSTATE_COMPLETE 
    Set HTMLDoc = MyBrowser.document 
    HTMLDoc.all.license_plate.Value = "LV11VYT" 

    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("button") '("input") 
    'Get 2nd button 
    If MyHTML_Element.Title = "Continue" Then 'MyHTML_Element.Click: Exit For 
    x = x + 1 
    If x = 2 Then 
    MyHTML_Element.Click 
    End If 
    End If 
    Next 
Err_Clear: 
    If Err <> 0 Then 
    Err.Clear 
    Resume Next 
    End If 
    End Sub 

現在我需要等待,直到刷新頁面,然後得到的結果,但我不能確定如何把結果出來

源代碼

<div id="block_subheader" class="block_editable block_wysiwyg"> 
<p>Almost there! <strong>TELL US</strong>&nbsp;which parts you need - <strong>ADD&nbsp;</strong>your contact details &amp; receive <strong>No Obligation Quotes</strong><span style="font-weight: normal;">&nbsp;to compare &amp; </span><span style="font-weight: normal;"><strong>Save &pound;&pound;'s!</strong></span></p>      
</div> 
<div class="clear"></div> 
<form id="step3" action="/find-a-part/step-3" method="post" enctype="multipart/form-data"> 
<div class="clearfix"> 
<h2>RENAULT MEGANE (X95) DYNAMIQUE TOMTOM DCI ECO 3 DOOR COUPE 1461cc (2011) DIESEL</h2> 
<p><a href="/find-a-part/step-2">Not quite the vehicle you're searching for? Click here to specify the vehicle exactly</a></p> 
</div> 

試圖獲得雷諾梅甘娜的詳細信息

任何人都可以幫忙嗎?

好的我已經過去了這部分,但遇到了另一個問題,當頁面更改點擊按鈕後,我需要更新html.document到新頁面,因爲當我在代碼中使用它時,它拉起舊的源代碼。

我可以得到它的工作,但它只適用於消息框激活說什麼瀏覽器名稱是。

有什麼建議嗎?

Dim HTMLDoc As HTMLDocument 
Dim MyBrowser As InternetExplorer 

Sub GetVehicleDetails2() 

    Dim MyHTML_Element As IHTMLElement 
    Dim HTMLDoc As HTMLDocument, Doc As HTMLDocument 
    Dim MyURL As String, Vehicle As String 
    Dim x As Integer, y As Integer 
    On Error GoTo Err_Clear 
    MyURL = "http://www.1stchoice.co.uk/find-a-part" 
    x = 0 
    'open new explorer 
    Set MyBrowser = New InternetExplorer 
    MyBrowser.Silent = True 
    'navigate to page 
    MyBrowser.navigate MyURL 
    MyBrowser.Visible = True 
    'wait until ready 
    Do While MyBrowser.Busy Or _ 
    MyBrowser.readyState <> 4 
    DoEvents 
    Loop 
    Do 
    Loop Until MyBrowser.readyState = READYSTATE_COMPLETE 
    Set HTMLDoc = MyBrowser.document 

    'enter registration in text box 
    HTMLDoc.all.license_plate.Value = "LV11VYT" 

    'click continue button 
    Set MyHTML_Element = HTMLDoc.getElementsByTagName("button")(1) 
    MyHTML_Element.Click 
    Set HTMLDoc = Nothing 
    'wait until page updated 

    Set Doc = MyBrowser.document 
    'Application.Wait (Now() + "00:00:05") 

    'does not work if you take this out 
    MsgBox MyBrowser.FullName 

    'find text returned with vehicle details 
    For Each MyHTML_Element In Doc.getElementsByTagName("form") 
     If MyHTML_Element.ID = "step3" Then 
     Vehicle = MyHTML_Element.innerText 
     MsgBox Vehicle 
     End If 
    Next 
    'close browser down 
'MyBrowser.Quit 

Err_Clear: 
    If Err <> 0 Then 
    Err.Clear 
    Resume Next 
    End If 
    End Sub 

使用2003或2007,嘗試網絡查詢,着通價值&使用繼續按鈕。

+0

請註明您的Excel版本。另外:你是否嘗試過使用wbe查詢?在某些情況下,他們會允許您只需要很少的編程就可以做你想做的事情! –

+0

我的意思是Excel * web *查詢 –

+2

+1(相比於一些新的海報,在這裏有一個體面的第一次傳遞:) –

回答

0

不用試圖從HTML中使用正則表達式(與解析器)提取元素,但Regex將是一種簡單的方法來提取您需要的元素,因爲它是定義良好的,並且只需要該元素。

你可以做這樣的事情(我提供只使用InStr函數的另一種方式,對於你的榜樣工作,但如果有很多結果立即退還或語法變化等則正則表達式會更靈活):

Sub blah() 

    Dim testStr As String 

    'test string you provided in the Question -> substitute it for your HTML return 
    testStr = ActiveSheet.Cells(1, 1).Value 

'Method 1: Use a simple Instr (fine for the example you provided, but if different bits you need to search are more complicated then you may need to use Regex instead 

    Dim startLocation As Long, endLocation As Long 
    Dim extractedText As String 

    startLocation = InStr(1, testStr, "<h2>", vbTextCompare) 

    If Not startLocation > 0 Then 

     Exit Sub 'or move to next or whatever 

    Else 

     endLocation = InStr(startLocation, testStr, "</h2>", vbTextCompare) 

     extractedText = Mid(testStr, startLocation + 4, endLocation - startLocation - 4) 

     Debug.Print "Basic InStr method: "; extractedText 

    End If 

'Method 2: Use Regex 

    'more flexible -> reference a Regex engine. 
    'This example uses Microsoft VBScript Regular Expressions 5.5 
    'That engine uses the same syntax as MS JavaScript regex 
    'See http://msdn.microsoft.com/en-us/library/1400241x.aspx for syntax 

    Dim regex As RegExp 
    Dim match As match 

    Set regex = New RegExp 

    With regex 

     .Pattern = "(?:<h2>)([\s\S]*?)(?=</h2>)" 
     'NB this regex engine does not support lookbehinds :-(
     'so we have to extract the submatched group for what we want 
     '(vs. just using Match.Value) 
     .IgnoreCase = True 
     .MultiLine = True 

     For Each match In .Execute(testStr) 

      Debug.Print "Regex match: "; match.SubMatches.Item(0) 

     Next match 

    End With 

End Sub 

輸出是:

Basic InStr method: RENAULT MEGANE (X95) DYNAMIQUE TOMTOM DCI ECO 3 DOOR COUPE 1461cc (2011) DIESEL 
Regex match: RENAULT MEGANE (X95) DYNAMIQUE TOMTOM DCI ECO 3 DOOR COUPE 1461cc (2011) DIESEL 
+0

嗨,謝謝,但我不需要從文本字符串中得到它。我試圖找出如何獲得顯示文本字符串的文檔的內文。我已經整理出來了,不過謝謝。 –