
Option Explicit
Dim FINE As Boolean
Const pagina = ... 'qui c'è l'ndirizzo di una pagina precisa
Private Sub cmdAggiorna_Click()
Dim r As Integer, p As Single
r = 2 ' prima riga in cui scrivere (in colonna 1)
Do While Cells(r, 1) <> "" ' ripeti fino alla fine delle righe occupate
Cells(r, 2).Font.Color = vbRed 'evidenzia in rosso il prezzo in corso di aggiornamento...
p = SingoloPrezzo(Cells(r, 1))
If p Then 'scrive il nuovo prezzo, SOLO se <> 0
Cells(r, 2) = p
Cells(r, 2).Font.Color = vbBlack 'se il dato e' stato aggiornato, riporta il colore a nero.
End If
r = r + 1
Loop
End Sub
Sub LanciaStringaSeguente10righe()
Dim URL As String, StrgPrec As String, i As Integer, s As String
URL = InputBox("Inserire URL")
StrgPrec = InputBox("Inserire Stringa Precedente")
For i = 1 To 10
s = StringaSeguente(URL, StrgPrec)
ActiveCell.Value = s
ActiveCell.Offset(1, 0).Select
StrgPrec = s
Next i
End Sub
Private Function StringaSeguente(URL As String, StrgPrec As String) As String '100 caratteri dopo la stringa di partenza
Dim txt As String ', URL As String
Dim p As Integer, s As String, pr As String, c As String
txt = LeggiPagina(URL)
s = StrgPrec ' stringa da cercare, che precede il dato che ci interessa
p = InStr(1, txt, s, vbTextCompare)
If p Then
p = p + Len(s) ' a partire da qui c'e' la parte che ci interessa
pr = ""
c = Mid$(txt, p, 100)
pr = pr & c
p = p + 1
StringaSeguente = pr
End If
End Function
Private Function SingoloPrezzo(ISIN As String) As String 'Single
Dim URL As String, txt As String
Dim p As Integer, s As String, pr As String, c As String
URL = pagina
txt = LeggiPagina(URL)
s = "ORDRE D'ARRIVÉE" ' stringa da cercare, che precede il dato che ci interessa
p = InStr(1, txt, s, vbTextCompare)
If p Then
p = p + Len(s) ' a partire da qui c'e' il prezzo
pr = ""
Do ' leggi il prezzo, carattere per carattere
c = Mid$(txt, p, 1)
'If InStr(1, "0123456789,", c) Then ' accetta cifre o virgola decimale
pr = pr & c
p = p + 1
'Else ' al primo carattere non accettabile esci dal loop (fine prezzo)
'Exit Do ' io invece non lo faccio uscire
'End If
Loop
SingoloPrezzo = pr
End If
End Function
Private Function LeggiPagina(URL As String) As String
' legge una singola pagina Web e ne restituisce il testo contenuto...
FINE = False
WB.Navigate URL
Do
DoEvents
Loop Until FINE
LeggiPagina = WB.Document.body.innertext
End Function
Private Sub WB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
' questo evento viene generato alla fine del caricamento di OGNI frame secondario.
' Per accertarsi che l'intera PAGINA e' stata caricata, bisognerebbe controllare il tipo di oggetto pDisp,
' ma apparentemente il VBA non lo permette (!?), pertanto possono risultare dei 'fine pagina' fittizi,
' col risultato di ottenere (a volte, specie in caso di apparizione di pop-up) dei dati errati o duplicati !
FINE = True
End Sub
Private Sub WB_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)
' aggiorna la percentuale di caricamento effettuato...
Dim p As Single
If ProgressMax Then
p = Progress / ProgressMax
If p > 1 Then p = 1
PROG.Caption = FormatPercent(p, 0)
End If
End Sub
Private Sub WB_StatusTextChange(ByVal Text As String)
' mostra i messaggi provenienti dal WebBrowser...
If Text <> "" Then MSG.Caption = Text
End Sub
|
Private Function LeggiPagina(URL As String) As String
' legge una singola pagina Web e ne restituisce il testo contenuto...
FINE = False
WB.Navigate URL
Do
DoEvents
Loop Until FINE
LeggiPagina = WB.Document.body.innertext
End Function |
Sub ParseHTML()
Dim wb As Workbook
Dim ws As Worksheet
Dim cella As Range
Dim ie As Object
Dim ie_Doc As Object
Dim ie_Element As Object
Dim ie_ParentEle As Object
Dim ie_ParentPar As Object
Dim nLoops As Long
Dim j As Long
Dim nData As Long
Dim sTesto As String
Const sWeb As String = "www.excelvba.it/Forum/forum.php?f=1"
On Error GoTo ParseHTML_Error
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Foglio1")
Set ie = CreateObject("InternetExplorer.Application")
Application.ScreenUpdating = False
With ie
.Silent = True
.Visible = False
.navigate sWeb
Application.Wait Now + TimeValue("0:00:02")
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
Set ie_Doc = .Document
Set cella = ws.Range("A4")
cella.Resize(ws.Cells(Rows.Count, 1).End(xlUp).Row + 1, 5).ClearContents
For Each ie_Element In ie_Doc.getElementsByTagName("a")
If ie_Element.className = "alink" Then
Debug.Print ie_Element.Attributes("href").Value
If ie_Element.Attributes("href").Value Like "*thread.php*" Then
Set ie_ParentEle = ie_Element.parentElement
cella.Offset(0, 0).Value = ie_Element.innerText
cella.Offset(0, 1).Value = ie_Element.Attributes("href").Value
ws.Hyperlinks.Add Anchor:=cella.Offset(0, 1), Address:=ie_Element.Attributes("href").Value, TextToDisplay:=ie_Element.innerText
sTesto = ie_ParentEle.innerText
sTesto = Trim(Replace(sTesto, ie_Element.innerText, ""))
cella.Offset(0, 2).Value = sTesto
For Each ie_ParentPar In ie_ParentEle.parentElement.getElementsByTagName("dd")
If ie_ParentPar.className = "posts" Then
cella.Offset(0, 3).Value = ie_ParentPar.innerText
ElseIf ie_ParentPar.className = "lastpost" Then
cella.Offset(0, 4).Value = ie_ParentPar.innerText
End If
Next
Set cella = cella.Offset(1, 0)
End If
End If
Next
End With
ParseHTML_Error:
Application.ScreenUpdating = True
Set cella = Nothing
Set ws = Nothing
Set wb = Nothing
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Description, vbCritical, "ERRORE"
Else
MsgBox "Elaborazione Terminata", vbInformation
End If
On Error Resume Next
ie.Quit
Set ie_Element = Nothing
Set ie_ParentEle = Nothing
Set ie_ParentPar = Nothing
Set ie_Doc = Nothing
Set ie = Nothing
End Sub
|
'---------------------------------------------------------------------------------------
' Procedure : prova
' Author : scossa
' Date : 18/03/2014
' Purpose :
'---------------------------------------------------------------------------------------
'
' attenzione: attivare i riferimenti a Microsoft Internet Controls
' e a Microsoft HTML Object Library
'
Sub ParseHTML()
Dim wb As Workbook
Dim ws As Worksheet
Dim cella As Range
Dim ie As InternetExplorer
Dim ie_Doc As HTMLDocument
Dim ie_Element As HTMLAnchorElement
Dim ie_ParentEle As HTMLDTElement
Dim ie_ParentPar As HTMLDListElement
Dim nLoops As Long
Dim sTesto As String
Const sWeb As String = "www.excelvba.it/Forum/forum.php?f=1"
On Error GoTo ParseHTML_Error
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Foglio1")
Set ie = CreateObject("InternetExplorer.Application")
Application.ScreenUpdating = False
With ie
.Silent = True
.Visible = False
.navigate sWeb
Application.Wait Now + TimeValue("0:00:02")
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
Set ie_Doc = .Document
Set cella = ws.Range("A4")
cella.Resize(ws.Cells(Rows.Count, 1).End(xlUp).Row + 1, 5).ClearContents
For Each ie_Element In ie_Doc.getElementsByTagName("a")
If ie_Element.className = "alink" Then
If ie_Element.Attributes("href").Value Like "*thread.php*" Then
Set ie_ParentEle = ie_Element.parentElement
cella.Offset(0, 0).Value = ie_Element.innerText
cella.Offset(0, 1).Value = ie_Element.Attributes("href").Value
ws.Hyperlinks.Add Anchor:=cella.Offset(0, 1), Address:=ie_Element.Attributes("href").Value, TextToDisplay:=ie_Element.innerText
sTesto = ie_ParentEle.innerText
sTesto = Trim(Replace(sTesto, ie_Element.innerText, ""))
cella.Offset(0, 2).Value = sTesto
For Each ie_ParentPar In ie_ParentEle.parentElement.getElementsByTagName("dd")
If ie_ParentPar.className = "posts" Then
cella.Offset(0, 3).Value = ie_ParentPar.innerText
ElseIf ie_ParentPar.className = "lastpost" Then
cella.Offset(0, 4).Value = ie_ParentPar.innerText
End If
Next
Set cella = cella.Offset(1, 0)
End If
End If
Next
End With
ParseHTML_Error:
Application.ScreenUpdating = True
Set cella = Nothing
Set ws = Nothing
Set wb = Nothing
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Description, vbCritical, "ERRORE"
Else
MsgBox "Elaborazione Terminata", vbInformation
End If
On Error Resume Next
ie.Quit
Set ie_Element = Nothing
Set ie_ParentEle = Nothing
Set ie_ParentPar = Nothing
Set ie_Doc = Nothing
Set ie = Nothing
End Sub
|
'---------------------------------------------------------------------------------------
' Procedure : prova
' Author : scossa
' Date : 18/03/2014
' Purpose :
'---------------------------------------------------------------------------------------
'
'
Sub ParseHTML()
Dim wb As Workbook
Dim ws As Worksheet
Dim cella As Range
Dim ie As Object
Dim ie_Doc As Object
Dim ie_Element As Object
Dim ie_ParentEle As Object
Dim ie_ParentPar As Object
Dim nLoops As Long
Dim sTesto As String
Const sWeb As String = "www.excelvba.it/Forum/forum.php?f=1"
On Error GoTo ParseHTML_Error
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Foglio1")
Set ie = CreateObject("InternetExplorer.Application")
Application.ScreenUpdating = False
With ie
.Silent = True
.Visible = False
.navigate sWeb
Application.Wait Now + TimeValue("0:00:02")
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
Set ie_Doc = .Document
Set cella = ws.Range("A4")
cella.Resize(ws.Cells(Rows.Count, 1).End(xlUp).Row + 1, 5).ClearContents
For Each ie_Element In ie_Doc.getElementsByTagName("a")
If ie_Element.className = "alink" Then
If ie_Element.href Like "*thread.php*" Then
Set ie_ParentEle = ie_Element.parentElement
cella.Offset(0, 0).Value = ie_Element.innerText
cella.Offset(0, 1).Value = ie_Element.href
ws.Hyperlinks.Add Anchor:=cella.Offset(0, 1), Address:=ie_Element.href, TextToDisplay:=ie_Element.innerText
sTesto = ie_ParentEle.innerText
sTesto = Trim(Replace(sTesto, ie_Element.innerText, ""))
cella.Offset(0, 2).Value = sTesto
For Each ie_ParentPar In ie_ParentEle.parentElement.getElementsByTagName("dd")
If ie_ParentPar.className = "posts" Then
cella.Offset(0, 3).Value = ie_ParentPar.innerText
ElseIf ie_ParentPar.className = "lastpost" Then
cella.Offset(0, 4).Value = ie_ParentPar.innerText
End If
Next
Set cella = cella.Offset(1, 0)
End If
End If
Next
End With
ParseHTML_Error:
Application.ScreenUpdating = True
Set cella = Nothing
Set ws = Nothing
Set wb = Nothing
If Err.Number <> 0 Then
MsgBox "Error: " & Err.Description, vbCritical, "ERRORE"
Else
MsgBox "Elaborazione Terminata", vbInformation
End If
On Error Resume Next
ie.Quit
Set ie_Element = Nothing
Set ie_ParentEle = Nothing
Set ie_ParentPar = Nothing
Set ie_Doc = Nothing
Set ie = Nothing
End Sub
|
