1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| Sub recherche_sur_page_web()
Dim AdresURL As String, SHtemp As Worksheet, c As Range, Result As String, Achercher As String
Dim MotCle As String, MoisDeb As String, JourDeb As String, MoisFin As String, JourFin As String
Dim TypRech As String, ParamRech As String, vartemp As String
Application.ScreenUpdating = False
MotCle = InputBox("Spécifiez le ou les mots à chercher" & Chr(10) _
& "Si plusieurs mots, séparez les par le signe +" & Chr(10) _
& "Exemple : mot1+mot2+mot3")
vartemp = InputBox("Spécifiez les dates de début et de fin de la recherche" & Chr(10) & Chr(10) _
& "Les dates doivent être au format jour/mois" & Chr(10) _
& "Les 2 dates sont séparées par un point-virgule ;" & Chr(10) _
& "Exemple : 1/9;4/9")
JourDeb = Split(Split(vartemp, ";")(0), "/")(0) '<-- jour du début de la recherche
MoisDeb = Split(Split(vartemp, ";")(0), "/")(1) '<-- mois du début de la recherche
JourFin = Split(Split(vartemp, ";")(1), "/")(0) '<-- jour de fin de recherche
MoisFin = Split(Split(vartemp, ";")(1), "/")(1) '<-- mois de fin de recherche
Achercher = "(Résultats *)" '<-- chaîne de caractères à chercher
reco:
'paramètre de la recherche
ParamRech = UCase(InputBox("Spécifiez le type de la recherche" & Chr(10) _
& "TMA = tous les mots n'importe où dans l'article" & Chr(10) _
& "TMT = tous les mots dans le titre de l'article" & Chr(10) _
& "PEA = phrase exacte n'importe où dans l'article" & Chr(10) _
& "PET = phrase exacte dans le titre de l'article" & Chr(10) _
& "1MA = au moins un des mots n'importe où dans l'article" & Chr(10) _
& "1MT = au moins un des mots dans le titre de l'article"))
Select Case ParamRech
Case "TMA"
TypRech = "&va=" & MotCle & "&va_vt=any&vp=&vp_vt=any&vo=&vo_vt=any&ve=&ve_vt=any"
Case "TMT"
TypRech = "&va=" & MotCle & "&va_vt=title&vp=&vp_vt=any&vo=&vo_vt=any&ve=&ve_vt=any"
Case "PEA"
TypRech = "&va=&va_vt=any&vp=" & MotCle & "&vp_vt=any&vo=&vo_vt=any&ve=&ve_vt=any"
Case "PET"
TypRech = "&va=&va_vt=any&vp=" & MotCle & "&vp_vt=title&vo=&vo_vt=any&ve=&ve_vt=any"
Case "1MA"
TypRech = "&va=&va_vt=any&vp=&vp_vt=any&vo=" & MotCle & "&vo_vt=any&ve=&ve_vt=any"
Case "1MT"
TypRech = "&va=&va_vt=any&vp=&vp_vt=any&vo=" & MotCle & "&vo_vt=title&ve=&ve_vt=any"
Case Else
MsgBox "Type de la recherche non valide" & Chr(10) _
& "Veuillez spécifier un des types suivants : TMA, TMT, PEA, PET, 1MA, 1MT", vbExclamation
GoTo reco
End Select
'adresse URL complète
AdresURL = "http://fr.news.search.yahoo.com/search/news?ei=UTF-8&.bcrumb=26eec6e9531c603084aa9963a226d75b%2C1220525810&fr=" & TypRech & "&datesort=&timeago=&pub=1&smonth=" & MoisDeb & "&sday=" & JourDeb & "&emonth=" & MoisFin & "&eday=" & JourFin & "&source=&location=&fl=0&n=15"
'création d'une feuille temporaire pour accueillir la requête
Set SHtemp = Sheets.Add(after:=Sheets(Sheets.Count))
'exécution de la requête
With SHtemp.QueryTables.Add(Connection:="URL;" & AdresURL, Destination:=Range("A1"))
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.Refresh BackgroundQuery:=False
End With
'recherche du résultat
Set c = SHtemp.Cells.Find(Achercher)
If Not c Is Nothing Then
Result = c.Text
Else
MsgBox "La recherche n'a donné aucun résultat"
End If
'suppression de la feuille temporaire
Application.DisplayAlerts = False
SHtemp.Delete
Application.DisplayAlerts = True
'affichage du résultat
If Result <> "" Then
Result = Split(Result, "sur ")(1)
Result = Left(Result, Len(Result) - 1)
MsgBox Result & " articles trouvés"
End If
'libération de la mémoire
Set SHtemp = Nothing
Set c = Nothing
Application.ScreenUpdating = True
End Sub |
Partager