使用vba从Web检索数据

时间:2013-08-03 15:36:46

标签: vba excel-vba excel

刚刚开始使用html,在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年,尝试过网络查询,无法传递价值&amp;使用继续按钮。

1 个答案:

答案 0 :(得分:0)

不使用Regex(而不是解析器)尝试从HTML中提取元素,但是Regex是一种提取所需元素的简单方法,因为它定义明确,您只需要该元素。

你可以做类似的事情(我提供了另一种方法,只使用InStr,适用于你的例子,但如果有很多结果一次返回或语法改变等,那么Regex会更灵活):

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