非常感谢您能帮助我建立/修复以下代码,以使用VBA IE DOM从https://www.webjet.com.au/flights/搜索航班信息。
假设我正在寻找5月30日从迪拜飞往伦敦的航班。
A)首先,我可以填写城市,但未从已保存的下拉内部列表中选择城市
B)如何从下拉列表中填写日期和其他信息(旅客人数,班级)?
Dim ie As SHDocVw.InternetExplorer
Dim htmlInput As MSHTML.IHTMLElement
Dim htmlbuttons As MSHTML.IHTMLElementCollection
Dim htmlbutton As MSHTML.IHTMLElement
Dim htmlAs As MSHTML.IHTMLElementCollection
Dim htmlA As MSHTML.IHTMLElement
Dim htmldoc As MSHTML.HTMLDocument
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.Navigate ("https://www.webjet.com.au/flights/")
Do While ie.ReadyState <> READYSTATE_COMPLETE
Loop
Debug.Print ie.LocationName, ie.LocationURL
Set htmldoc = ie.Document
Set htmlInput = htmldoc.getElementById("departure-airport")
htmlInput.Value = "Dubai"
' << need help here how input will search for full city and airport name from its drop down list
Set htmlInput = htmldoc.getElementById("destination-airport")
htmlInput.Value = "london" ' << need help here how input will search for full city and airport name from its drop down list
' ** addional code needed to choose number of passenger, class and date.
For Each htmlA In htmlAs ' this code to choose one way or two way which is fine..
If i = 32 Then
htmlA.Click
Exit For
End If
i = i + 1
Next htmlA
Set htmlbutton = htmldoc.getElementById("search-btn")
htmlbutton.Click
答案 0 :(得分:0)
XHR:
您可以使用xhr并模仿页面发出的POST请求以获取航班信息。您的选择,例如成人人数,目的地.....进入POST正文。您会收到包含所有信息的json响应。我使用this json解析器处理响应并生成要使用的json对象。
浏览json响应here。
以下为1位成人往返的航班,迪拜所有机场,伦敦所有选定机场,经济舱。您可以将搜索条件中的值串联起来。
POST请求返回的json的快照比较(显示了通过基加利飞往伦敦的航班的第一航段的部分
Option Explicit
Public Sub GetFlights()
'install code from https://github.com/VBA-tools/VBA-JSON/blob/master/JsonConverter.bas to a module in project
'VBE > Tools > References > Add reference to Microsoft Scripting Runtime
Dim json As Object
Dim body As String
body = "{""TripType"":""Return"",""Adults"":""1"",""Children"":""0"","
body = body & """Infants"":""0"",""Class"":""Economy"","
body = body & """FlightStops"":[{""CityFromCode"":""DXB"",""CityToCode"":""LON"",""DepartureDate"":""2019-05-30"",""ReturnDate"":""2019-06-01"",""TsaAirportFromCode"":""DXBALL"",""TsaAirportToCode"":""LON""}],"
body = body & """ShowMixAndMatch"":false,""Locale"":""en-AU""}"
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", "https://flights.webjet.com.au/api/HybridSearch/GetFlightSearchResults?instart_disable_injection=true", False
.setRequestHeader "Accept", "application/json, text/plain, */*"
.send body
Set json = JsonConverter.ParseJson(.responseText)
End With
Stop
End Sub
Internet Explorer:
您可以通过javascript设置日期(以便保留输入内容)
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetPrices()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.webjet.com.au/flights/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .querySelector("#departure-airport")
.Focus
.Value = "Dubai, United Arab Emirates - All Airports (DXB)"
End With
Application.Wait Now + TimeSerial(0, 0, 1)
With .querySelector("#destination-airport")
.Focus
.Value = "London, United Kingdom - All Airports (LON)"
End With
Application.Wait Now + TimeSerial(0, 0, 1)
With .querySelector("#ft-dates-return")
.Focus
.Value = "Thu 30 May 2019 - Tue 4 Jun 2019"
.FireEvent "onchange"
End With
.parentWindow.execScript "document.querySelector('#ft-hidden-check-in-date-return').value = 'Thu 30 May 2019';"
.parentWindow.execScript "document.querySelector('#ft-hidden-check-out-date-return').value = 'Tue 4 Jun 2019';"
.parentWindow.execScript "document.querySelector('#ft-hidden-popover-date-return').value = 'Thu 30 May 2019 - Tue 4 Jun 2019';"
Application.Wait Now + TimeSerial(0, 0, 1)
.querySelector("#zsl-button-search").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop
.Quit
End With
End Sub
与大人,孩子一起...
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetPrices()
Dim html As HTMLDocument, ie As New InternetExplorer
Set html = New HTMLDocument
Const ADULTS As Long = 2
Const CHILDREN As Long = 2
Const INFANTS As Long = 1 'infants cannot be greater than number of adults
With ie
.Visible = True
.Navigate2 "https://www.webjet.com.au/flights/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .querySelector("#departure-airport")
.Focus
.Value = "Dubai, United Arab Emirates - All Airports (DXB)"
End With
Application.Wait Now + TimeSerial(0, 0, 1)
With .querySelector("#destination-airport")
.Focus
.Value = "London, United Kingdom - All Airports (LON)"
End With
Application.Wait Now + TimeSerial(0, 0, 1)
With .querySelector("#ft-dates-return")
.Focus
.Value = "Thu 30 May 2019 - Tue 4 Jun 2019"
.FireEvent "onchange"
End With
.parentWindow.execScript "document.querySelector('#ft-hidden-check-in-date-return').value = 'Thu 30 May 2019';"
.parentWindow.execScript "document.querySelector('#ft-hidden-check-out-date-return').value = 'Tue 4 Jun 2019';"
.parentWindow.execScript "document.querySelector('#ft-hidden-popover-date-return').value = 'Thu 30 May 2019 - Tue 4 Jun 2019';"
Application.Wait Now + TimeSerial(0, 0, 1)
.querySelector("#button-passengers .zsl-caret-down").Click
Do
.querySelector("[aria-label='Increase adult passengers']").Click
Loop Until .querySelector("#number-of-adults") = ADULTS
Do
.querySelector("[aria-label='Increase child passengers 2-11 years old']").Click
Loop Until .querySelector("#number-of-children") = CHILDREN
Do
.querySelector("[aria-label='Increase infant passengers less than 2 years old']").Click
Loop Until .querySelector("#number-of-infants") = IIf(INFANTS > ADULTS, ADULTS, INFANTS)
Application.Wait Now + TimeSerial(0, 0, 1)
.querySelector("#zsl-button-search").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop
.Quit
End With
End Sub