-Aperçu- Je l'ai fait parce que je l'ai souvent utilisé dans les affaires.
Selenium est un outil qui automatise facilement les opérations du navigateur. J'entends souvent comment utiliser Selenium avec Python ou JavaScript, mais en fait, il peut être exécuté avec Visual Basic.
Surtout au Japon, je pense que de nombreuses entreprises sont fortement dépendantes d'Excel, donc utiliser Excel comme interface graphique ne semble pas avoir de résistance psychologique. (Je pense que j'ai beaucoup à dire ...)
-procédure-
-Détails- 1.2. L'URL etc. peut changer. Veuillez vérifier chacun d'eux. Vous pouvez l'installer par défaut. 3. Là encore, l'URL peut changer. Veuillez vérifier chacun d'eux. Pour Chrome, veuillez télécharger la même version de Chrome que vous utilisez. 4. Remplacez simplement. 5.
Si vous passez à 4, la bibliothèque de types de sélénium apparaîtra dans Outils-> Paramètres de référence dans l'environnement de développement Excel, alors vérifiez-le. Si vous ne le faites pas, vous ne pourrez pas utiliser Selenium. Vérifiez également les paramètres de référence tels que Microsoft Scripting Runtime.
Voici un exemple. à ~ (enregistrer le répertoire de destination, enregistrer le nom du fichier, l'URL de la page Web, le numéro de ligne de la feuille (pour la confirmation ou l'écriture du répertoire de sauvegarde), la feuille à gérer si elle est enregistrée ou non)
Pour PDF
Option Explicit
Sub toPDF(ByVal directory, ByVal filename, ByVal url, ByVal i, ByVal sheetn As String)
On Error GoTo myerror:
Dim sheet1 As Worksheet
Set sheet1 = Sheets(sheetn)
Dim sPath As String, WSH As Variant
Set WSH = CreateObject("WScript.Shell")
sPath = WSH.SpecialFolders("Desktop") & "\"
If (directory = "") Then directory = sPath
If (Right(directory, 1) <> "\") Then directory = directory & "\"
If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
Dim savepath As String
savepath = directory & filename & ".pdf"
Dim driver As New Selenium.ChromeDriver
driver.SetPreference "download.default_directory", directory
driver.SetPreference "download.directory_upgrade", True
driver.SetPreference "download.prompt_for_download", False
driver.SetPreference "safebrowsing.enabled", True
driver.SetPreference "plugins.plugins_disabled", Array("Chrome PDF Viewer")
driver.AddArgument "headless"
driver.AddArgument "disable-gpu"
driver.AddArgument "hide-scrollbars"
Dim w As Long
Dim h As Long
driver.Start
driver.Get url
w = driver.ExecuteScript("return document.body.scrollWidth")
h = driver.ExecuteScript("return document.body.scrollHeight")
Dim pdf As Object
driver.Window.SetSize w, h
Set pdf = CreateObject("Selenium.PdfFile")
pdf.SetPageSize 210, 297, "mm"
pdf.AddImage driver.TakeScreenshot, True
pdf.SaveAs savepath
sheet1.Cells(i, 5).Value = 1
sheet1.Cells(i, 6).Value = savepath
driver.Quit
Exit Sub
myerror:
MsgBox "no"
sheet1.Cells(i, 5).Value = 0
End Sub
Sub dopdf()
Dim sPath As String, WSH As Variant
Set WSH = CreateObject("WScript.Shell")
sPath = WSH.SpecialFolders("Desktop") & "\"
Dim directory As String
Dim filename As String
directory = sPath
If (Right(Len(directory), 1) <> "\") Then directory = directory & "\"
Dim sheet1 As String
Dim i As Long
sheet1 = "Pour pdf"
Dim sheetn As Worksheet
Set sheetn = Sheets(sheet1)
Dim r As Long
r = sheetn.Cells(Rows.Count, 4).End(xlUp).Row
For i = 2 To r
If (sheetn.Cells(i, 4).Value = "") Then
GoTo a1:
End If
filename = sheetn.Cells(i, 3).Text
If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
Dim result
result = Dir(sheetn.Cells(i, 2).Text, vbDirectory)
If (directory <> "" Or result <> True) Then sPath = sheetn.Cells(i, 2).Text
Call toPDF(sPath, filename, sheetn.Cells(i, 4).Text, i, sheet1)
a1:
Next i
End Sub
Option Explicit
Sub toJPG(ByVal directory, ByVal filename, ByVal url, ByVal i, ByVal sheetn As String)
On Error GoTo myerror:
Dim sheet1 As Worksheet
Set sheet1 = Sheets(sheetn)
Dim sPath As String, WSH As Variant
Set WSH = CreateObject("WScript.Shell")
sPath = WSH.SpecialFolders("Desktop") & "\"
If (directory = "") Then directory = sPath
If (Right(directory, 1) <> "\") Then directory = directory & "\"
If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
Dim savepath As String
savepath = directory & filename & ".jpg "
Dim driver As New Selenium.ChromeDriver
driver.SetPreference "download.default_directory", directory
driver.SetPreference "download.directory_upgrade", True
driver.SetPreference "download.prompt_for_download", False
driver.SetPreference "safebrowsing.enabled", True
driver.SetPreference "plugins.plugins_disabled", Array("Chrome PDF Viewer")
driver.AddArgument "headless"
driver.AddArgument "disable-gpu"
driver.AddArgument "hide-scrollbars"
Dim w As Long
Dim h As Long
driver.Start
driver.Get url
driver.FindElementByClass("tab02").Click
driver.ExecuteScript ("this.document.getElementById('tab01').setAttribute('class','tabContent01');")
driver.ExecuteScript ("this.document.getElementById('tab03').setAttribute('class','tabContent03');")
w = driver.ExecuteScript("return document.body.scrollWidth")
h = driver.ExecuteScript("return document.body.scrollHeight")
driver.Window.SetSize w, h
driver.TakeScreenshot.SaveAs savepath
sheet1.Cells(i, 5).Value = 1
sheet1.Cells(i, 6).Value = savepath
driver.Quit
Exit Sub
myerror:
sheet1.Cells(i, 5).Value = 0
End Sub
Sub dojpg()
Dim sPath As String, WSH As Variant
Set WSH = CreateObject("WScript.Shell")
sPath = WSH.SpecialFolders("Desktop") & "\"
Dim directory As String
Dim filename As String
directory = sPath
If (Right(Len(directory), 1) <> "\") Then directory = directory & "\"
Dim sheet1 As String
Dim i As Long
sheet1 = "Pour jpg"
Dim sheetn As Worksheet
Set sheetn = Sheets(sheet1)
Dim r As Long
r = sheetn.Cells(Rows.Count, 4).End(xlUp).Row
For i = 2 To r
If (sheetn.Cells(i, 4).Value = "") Then
GoTo a1:
End If
filename = sheetn.Cells(i, 3).Text
If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
Dim result
result = Dir(sheetn.Cells(i, 2).Text, vbDirectory)
If (directory <> "" Or result <> True) Then sPath = sheetn.Cells(i, 2).Text
Call toJPG(sPath, filename, sheetn.Cells(i, 4).Text, i, sheet1)
a1:
Next i
End Sub
Option Explicit
Sub toPNG(ByVal directory, ByVal filename, ByVal url, ByVal i, ByVal sheetn As String)
On Error GoTo myerror:
Dim sheet1 As Worksheet
Set sheet1 = Sheets(sheetn)
Dim sPath As String, WSH As Variant
Set WSH = CreateObject("WScript.Shell")
sPath = WSH.SpecialFolders("Desktop") & "\"
If (directory = "") Then directory = sPath
If (Right(directory, 1) <> "\") Then directory = directory & "\"
If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
Dim savepath As String
savepath = directory & filename & ".png "
Dim driver As New Selenium.ChromeDriver
driver.SetPreference "download.default_directory", directory
driver.SetPreference "download.directory_upgrade", True
driver.SetPreference "download.prompt_for_download", False
driver.SetPreference "safebrowsing.enabled", True
driver.SetPreference "plugins.plugins_disabled", Array("Chrome PDF Viewer")
driver.AddArgument "headless"
driver.AddArgument "disable-gpu"
driver.AddArgument "hide-scrollbars"
Dim w As Long
Dim h As Long
driver.Start
driver.Get url
driver.FindElementByClass("tab02").Click
driver.ExecuteScript ("this.document.getElementById('tab01').setAttribute('class','tabContent01');")
driver.ExecuteScript ("this.document.getElementById('tab03').setAttribute('class','tabContent03');")
w = driver.ExecuteScript("return document.body.scrollWidth")
h = driver.ExecuteScript("return document.body.scrollHeight")
driver.Window.SetSize w, h
driver.TakeScreenshot.SaveAs savepath
sheet1.Cells(i, 5).Value = 1
sheet1.Cells(i, 6).Value = savepath
driver.Quit
Exit Sub
myerror:
sheet1.Cells(i, 5).Value = 0
End Sub
Sub dopng()
Dim sPath As String, WSH As Variant
Set WSH = CreateObject("WScript.Shell")
sPath = WSH.SpecialFolders("Desktop") & "\"
Dim directory As String
Dim filename As String
directory = sPath
If (Right(Len(directory), 1) <> "\") Then directory = directory & "\"
Dim sheet1 As String
Dim i As Long
sheet1 = "Pour png"
Dim sheetn As Worksheet
Set sheetn = Sheets(sheet1)
Dim r As Long
r = sheetn.Cells(Rows.Count, 4).End(xlUp).Row
For i = 2 To r
If (sheetn.Cells(i, 4).Value = "") Then
GoTo a1:
End If
filename = sheetn.Cells(i, 3).Text
If (filename = "") Then filename = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
Dim result
result = Dir(sheetn.Cells(i, 2).Text, vbDirectory)
If (directory <> "" Or result <> True) Then sPath = sheetn.Cells(i, 2).Text
Call toPNG(sPath, filename, sheetn.Cells(i, 4).Text, i, sheet1)
a1:
Next i
End Sub
La structure du livre se compose de trois feuilles «pdf», «png» et «jpg».
Colonnes B, C, D après la deuxième ligne Entrez la destination d'enregistrement, le nom d'enregistrement et l'URL Il est enregistré lorsque vous déplacez la macro.
Au fait, vous pouvez également manipuler le DOM avec driver.executescirpts etc., afin de pouvoir capturer la page Web après avoir joué avec.
somnolent. .. .. Aussi, je joindrai une photo plus tard.
Recommended Posts