刚开始使用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> which parts you need - <strong>ADD </strong>your contact details & receive <strong>No Obligation Quotes</strong><span style="font-weight: normal;"> to compare & </span><span style="font-weight: normal;"><strong>Save ££'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>
试图获得雷诺Megane的详细
有人能帮忙吗?
好的,我已经通过了这一部分,但遇到了另一个问题,当点击按钮后页面发生变化时,我需要将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,尝试过web查询,不能传递值&使用"继续"按钮。
不需要开始争论使用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>)([sS]*?)(?=</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