Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Public Sub WriteOutShipInspectionTable()
- Dim http As Object, s As String, ws As Worksheet, re As Object
- Set http = CreateObject("MSXML2.XMLHTTP")
- Set ws = ThisWorkbook.Worksheets("Sheet1")
- Set re = CreateObject("VBScript.RegExp")
- Dim html As HTMLDocument, body As String, headers(), startDate As String, endDate As String
- startDate = "01.08.2018"
- endDate = "31.08.2019"
- headers = Array("IMO Number", "Ship Name", "Flag state", "Ship Type", "Date of inspection", "Place of inspection", "AdditCol1", "AdditCol2")
- Set html = New MSHTML.HTMLDocument
- With re
- .Global = True
- .MultiLine = True
- End With
- With http
- .Open "POST", "http://www.medmouic.org/Home/Trouver", False
- .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
- .send "imonumber=&val=0&Name=&selectFlag=333&selectType=&date1=" & startDate & "&date2=" & endDate
- s = .responseText
- html.body.innerHTML = GetString(re, s, "(<table[\s\S]*?<\/table>)")
- Dim totalInspections As Long, results(), r As Long, offset As Long
- totalInspections = CLng(GetString(re, s, "'anyDiv', '(\d+)'"))
- ReDim results(1 To totalInspections, 1 To UBound(headers) + 1)
- results = PopulateArray(http, html, r, results)
- For offset = 10 To totalInspections Step 10
- .Open "POST", "http://www.medmouic.org/Home/Trouver", False
- .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
- .send "imonumber=&val=" & CStr(offset) & "&Name=&selectFlag=333&selectType=&date1=" & startDate & "&date2=" & endDate
- s = .responseText
- html.body.innerHTML = GetString(re, s, "(<table[\s\S]*?<\/table>)")
- results = PopulateArray(http, html, r, results)
- Next
- End With
- With ws
- .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
- .Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
- End With
- End Sub
- Public Function GetAdditionalColumns(ByVal http, ByVal url As String) As Variant
- Dim results(0 To 1)
- With http
- .Open
- .GET , url, "false"
- 'do something with response regex if required or read into htmlDocument
- 'extract two items of interest result0, result1
- results(0) = result0
- results(1) = result1
- End With
- GetAdditionalColumns = results
- End Function
- Public Function PopulateArray(ByVal http As Object, ByVal html As MSHTML.HTMLDocument, ByRef r As Long, ByRef results As Variant) As Variant
- Dim c As Long, tr As MSHTML.HTMLTableRow, td As MSHTML.HTMLTableCell, i As Long
- For i = 1 To html.querySelectorAll("tr").Length - 1
- r = r + 1: c = 1
- For Each td In html.querySelectorAll("tr").Item(i).getElementsByTagName("td")
- Select Case c
- Case hrefColumnNumber 'the href column
- Dim href As String, additionalColumns()
- href = html.querySelectorAll("tr").Item(i).getElementsByTagName("td").href
- additionalColumns = GetAdditionalColumns(http, href)
- results(r, 7) = additionalColumns(0)
- results(r, 8) = additionalColumns(1)
- Case Else
- results(r, c) = td.innerText
- End Select
- c = c + 1
- Next
- Next
- PopulateArray = results
- End Function
- Public Function GetString(ByVal re As Object, ByVal s As String, ByVal p As String) As String
- With re
- .Pattern = p
- GetString = .Execute(s)(0).submatches(0)
- End With
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement