Sub ParseHTMLRegEx()
Dim wb As Workbook
Dim ws As Worksheet
Dim cella As Range
Dim ie As Object
Dim ie_Element As Object
Dim nLoops As Long
Dim re As Object
Dim reMatch As Variant
Dim nItems As Long
Dim j As Long
Dim nStart As Long
Dim nEnd As Long
Dim sTesto As String
Dim sParsed As String
Dim sToken As String
Dim sPatt As String
Const sWeb As String = "####://scossavr.altervista.org/Downloads/Excel/filebuono.html" 'sostituire con il tuo indirizzo web
Const sID = "_ContentPlaceHolder1_GridView1_ct"
On Error GoTo parseHTML_Error
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Foglio2")
Set re = CreateObject("vbscript.regexp")
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.navigate sWeb
Application.Wait Now + TimeValue("0:00:03")
nLoops = 0
Do Until .ReadyState = 4
DoEvents
nLoops = nLoops + 1
If nLoops > 50000 Then
Err.Raise vbObjectError + 513, , "Il server non risponde"
End If
Loop
sTesto = .document.body.innerHTML
End With
ie.Quit
With re
.Global = True
.IgnoreCase = True
'--------- NUM ----------------------------
Set cella = ws.Cells(ws.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
sPatt = "GridView1_ctldd_NUM>"
.Pattern = sPatt
Set reMatch = .Execute(sTesto)
With reMatch
nItems = .Count
If nItems > 0 Then
For j = 0 To nItems - 1
nStart = .item(j).Firstindex + .item(j).Length + 1
nEnd = InStr(nStart, sTesto, "", vbTextCompare)
sParsed = Mid(sTesto, nStart, nEnd - nStart)
cella.Value = sParsed
Set cella = cella.Offset(1, 0)
Next
End If
End With
'--------- cognome ----------------------------
Set cella = ws.Cells(ws.Cells(Rows.Count, 2).End(xlUp).Row + 1, 2)
sPatt = "GridView1_ctldd_cognome>"
.Pattern = sPatt
Set reMatch = .Execute(sTesto)
With reMatch
nItems = .Count
If nItems > 0 Then
For j = 0 To nItems - 1
nStart = .item(j).Firstindex + .item(j).Length + 1
nEnd = InStr(nStart, sTesto, "", vbTextCompare)
sParsed = Mid(sTesto, nStart, nEnd - nStart)
cella.Value = sParsed
Set cella = cella.Offset(1, 0)
'End If
Next
End If
End With
'--------- nome ----------------------------
Set cella = ws.Cells(ws.Cells(Rows.Count, 3).End(xlUp).Row + 1, 3)
sPatt = "GridView1_ctldd_nome>"
.Pattern = sPatt
Set reMatch = .Execute(sTesto)
With reMatch
nItems = .Count
If nItems > 0 Then
For j = 0 To nItems - 1
nStart = .item(j).Firstindex + .item(j).Length + 1
nEnd = InStr(nStart, sTesto, "", vbTextCompare)
sParsed = Mid(sTesto, nStart, nEnd - nStart)
'If sParsed <> "" Then
cella.Value = sParsed
Set cella = cella.Offset(1, 0)
Next
End If
End With
'--------- inizio ----------------------------
Set cella = ws.Cells(ws.Cells(Rows.Count, 4).End(xlUp).Row + 1, 4)
sPatt = "GridView1_ctldd_inizio>"
.Pattern = sPatt
Set reMatch = .Execute(sTesto)
With reMatch
nItems = .Count
If nItems > 0 Then
For j = 0 To nItems - 1
nStart = .item(j).Firstindex + .item(j).Length + 1
nEnd = InStr(nStart, sTesto, "", vbTextCompare)
sParsed = Mid(sTesto, nStart, nEnd - nStart)
cella.Value = sParsed
Set cella = cella.Offset(1, 0)
Next
End If
End With
'--------- termine ----------------------------
Set cella = ws.Cells(ws.Cells(Rows.Count, 5).End(xlUp).Row + 1, 5)
sPatt = "GridView1_ctldd_termine>"
.Pattern = sPatt
Set reMatch = .Execute(sTesto)
With reMatch
nItems = .Count
If nItems > 0 Then
For j = 0 To nItems - 1
nStart = .item(j).Firstindex + .item(j).Length + 1
nEnd = InStr(nStart, sTesto, "", vbTextCompare)
sParsed = Mid(sTesto, nStart, nEnd - nStart)
cella.Value = sParsed
Set cella = cella.Offset(1, 0)
Next
End If
End With
End With
parseHTML_Error:
Set cella = Nothing
Set ws = Nothing
Set wb = Nothing
Set re = Nothing
Set reMatch = Nothing
Set ie = Nothing
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Description, vbCritical, "ERRORE"
Else
MsgBox "Elaborazione Terminata", vbInformation
End If
End Sub
Sub ParseHTML()
Dim wb As Workbook
Dim ws As Worksheet
Dim cella As Range
Dim ie As Object
Dim ie_Element As Object
Dim nLoops As Long
Dim re As Object
Dim reMatch As Variant
Dim nItems As Long
Dim j As Long
Dim nStart As Long
Dim nEnd As Long
Dim sTesto As String
Dim sParsed As String
Dim sToken As String
Dim sPatt As String
Const sWeb As String = "h##p://scossavr.altervista.org/Downloads/Excel/filebuono.html" 'sostituire con il tuo indirizzo web
Const sID = "_ContentPlaceHolder1_GridView1_ct" ' attivare al posto di quella sotto
'Const sID = "footer"
On Error GoTo parseHTML_Error
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Foglio2")
Set re = CreateObject("vbscript.regexp")
Set ie = CreateObject("InternetExplorer.Application")
Set cella = ws.Cells(ws.Cells(Rows.Count, 1).End(xlUp).Row, 1)
With ie
.Silent = True
.Visible = False 'True
.navigate sWeb
Application.Wait Now + TimeValue("0:00:03")
nLoops = 0
Do Until .ReadyState = 4
DoEvents
nLoops = nLoops + 1
If nLoops > 50000 Then
Err.Raise vbObjectError + 513, , "Il server non risponde"
End If
Loop
'sTesto = .document.body.innerHTML
For Each ie_Element In .document.getElementsByTagName("SPAN")
Debug.Print ie_Element.ID
Select Case True
Case InStr(1, ie_Element.ID, "_NUM", vbBinaryCompare) > 0
sTesto = ie_Element.innerText
j = j + 1
cella.Offset(j, 0).Value = sTesto
Case InStr(1, ie_Element.ID, "_cognome", vbBinaryCompare) > 0
sTesto = ie_Element.innerText
cella.Offset(j, 1).Value = sTesto
Case InStr(1, ie_Element.ID, "_nome", vbBinaryCompare) > 0
sTesto = ie_Element.innerText
cella.Offset(j, 2).Value = sTesto
Case InStr(1, ie_Element.ID, "_inizio", vbBinaryCompare) > 0
sTesto = ie_Element.innerText
cella.Offset(j, 3).Value = sTesto
Case InStr(1, ie_Element.ID, "_termine", vbBinaryCompare) > 0
sTesto = ie_Element.innerText
cella.Offset(j, 4).Value = sTesto
End Select
Next
End With
ie.Quit
parseHTML_Error:
Set cella = Nothing
Set ws = Nothing
Set wb = Nothing
Set re = Nothing
Set reMatch = Nothing
Set ie = Nothing
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Description, vbCritical, "ERRORE"
Else
MsgBox "Elaborazione Terminata", vbInformation
End If
End Sub
|