Le scénario ci-dessous consiste à s'identifier sur le site d' investir pour y télécharger l'hebdomadaire au format éléctronique disponible dès le Vendredi soir. La problématique consistait à conserver les cookies de sessions et à analyser la page contenant les noms des fichiers à télécharger.
Les étapes sont :
- Connection à la page d'accueil pour obtenir un cookie de session
- Poster les données d'identification avec les cookies obtenus précédement
- Télécharger la page contenant la liste des fichiers PDF
- Analyser le résultat avec une expression rationnelle
- Télécharger chaque fichier PDF
' GetInvestir.vbs
' Script permettant la récupération par script de
' l'ensemble des fichiers PDF du magazine investir
'
Const ForReading = 1
Const WindowStyle = 10
set objFS=CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject ("WSCript.shell")
Set regEx = New RegExp
objShell.run "curl -c cookie.txt -G ""www.investir.fr""",WindowStyle,true
objShell.run "curl -b cookie.txt -c cookie.txt -d ""login=INDETIFIANT&password=MOTDEPASSE"" www.investir.fr/log_membre.phtml",WindowStyle,true
objShell.run "curl -b cookie.txt -G ""www.investir.fr/club_investir/hebdo_pdf.phtml"" -o hebdo_pdf.phtml",WindowStyle,true
Set objFile = objFS.OpenTextFile("hebdo_pdf.phtml", ForReading, True)
strHTML = objFile.ReadAll
regEx.Pattern = "telechargement_pdf.phtml\?(?:type=\D+&|)f=((page|cote)_\d+.pdf)&c=\d+&z=\d+"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strHTML)
For Each Match in Matches
wscript.echo "Getting page "&Match.SubMatches(0)&" ..."
objShell.run "curl -b cookie.txt -G ""www.investir.fr/club_investir/"&Match.Value&""" -o "&Match.SubMatches(0),WindowStyle,true
Next
C:\Temp>GetInvestir.vbs
Getting page page_01.pdf ...
Getting page page_40.pdf ...
Getting page page_36.pdf ...
Getting page page_37.pdf ...
Getting page page_04.pdf ...
Getting page page_05.pdf ...
Getting page page_38.pdf ...
Getting page page_39.pdf ...
Getting page page_02.pdf ...
Getting page page_03.pdf ...
Getting page page_06.pdf ...
L'ensemble des fichiers PDF est maintenant disponible dans le répertoire courant.
Si vous possédez Acrobat, il vous est possible de fusionner automatiquement ces fichiers en un fichier unique par le script suivant :
' FusionPDF.vbs
' Script de fusion des fichiers PDF d'investir
' Il est arrivé qu'il manque par erreur un fichier PDF c'est pourquoi
' La fusion de fait par itération sur les 55 noms de fichiers possibles
Const DOC_FOLDER = "C:\Temp\"
Set Acroapp = CreateObject("AcroExch.App")
Acroapp.Hide
Set avCodeFile = CreateObject("AcroExch.AVDoc")
Set avFormCapture = CreateObject("AcroExch.AVDoc")
set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
' Page_XX.pdf
iFile=1
iPage=1
strFilename = DOC_FOLDER & "page_"&right("0"&iFile,2)&".pdf"
do while iFile<55
if objFSO.FileExists(strFilename) then
wscript.echo strFilename
if iPage=1 then
avCodeFile.Open strFilename, "Investir.pdf"
Set pdCodeFile = avCodeFile.GetPDDoc
else
avFormCapture.Open strFilename, ""
Set pdFormCapture = avFormCapture.GetPDDoc
Set AVPage = avCodeFile.GetAVPageView
pdCodeFile.InsertPages iPage-2, pdFormCapture, 0, 1, 0
pdFormCapture.Close
avFormCapture.Close 1
Set pdFormCapture = Nothing
end if
iPage=iPage+1
end if
iFile=iFile+1
strFilename = DOC_FOLDER & "page_"&right("0"&iFile,2)&".pdf"
loop
' Cote_XX.pdf
iFile=1
strFilename = DOC_FOLDER & "Cote_"&right("0"&iFile,2)&".pdf"
do while iFile<55
if objFSO.FileExists(strFilename) then
wscript.echo strFilename
avFormCapture.Open strFilename, ""
Set pdFormCapture = avFormCapture.GetPDDoc
Set AVPage = avCodeFile.GetAVPageView
pdCodeFile.InsertPages iPage-2, pdFormCapture, 0, 1, 0
pdFormCapture.Close
avFormCapture.Close 1
Set pdFormCapture = Nothing
iPage=iPage+1
end if
iFile=iFile+1
strFilename = DOC_FOLDER & "Cote_"&right("0"&iFile,2)&".pdf"
loop
Set pdDoc = avCodeFile.GetPDDoc
pdDoc.Save 1, DOC_FOLDER&"\Investir.pdf"
pdCodeFile.Close
avCodeFile.Close 0
'Exit Acrobat
Acroapp.Exit