-Überblick- Ich habe es gemacht, weil ich es oft im Geschäft benutzt habe.
Selen ist ein Tool, das Browseroperationen einfach automatisiert. Ich höre oft, wie man Selenium mit Python oder JavaScript verwendet, aber tatsächlich kann es mit Visual Basic ausgeführt werden.
Insbesondere in Japan denke ich, dass viele Unternehmen stark von Excel abhängig sind. Daher scheint die Verwendung von Excel als grafische Benutzeroberfläche keinen psychologischen Widerstand zu haben. (Ich glaube ich habe viel zu sagen ...)
-Verfahren-
-Einzelheiten- 1.2. URL etc. kann sich ändern. Bitte überprüfen Sie jeden einzelnen. Sie können es standardmäßig installieren. 3. Auch hier kann sich die URL ändern. Bitte überprüfen Sie jeden einzelnen. Laden Sie für Chrome dieselbe Version von Chrome herunter, die Sie verwenden. 4. Einfach ersetzen. 5.
Wenn Sie auf 4 gehen, wird die Selenium-Typbibliothek in der Excel-Entwicklungsumgebung unter Extras-> Referenzeinstellungen angezeigt. Überprüfen Sie sie daher. Wenn Sie dies nicht tun, können Sie Selen nicht verwenden. Überprüfen Sie auch die Referenzeinstellungen wie Microsoft Scripting Runtime.
Unten ist ein Beispiel. bis ~ (Zielverzeichnis speichern, Dateiname speichern, Webseiten-URL, Blattzeilennummer (zur Bestätigung oder zum Schreiben des Speicherverzeichnisses), Blatt zum Verwalten, ob gespeichert oder nicht)
Für 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 = "Für 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 = "Für 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 = "Für 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
Die Buchstruktur besteht aus drei "pdf" -, "png" - und "jpg" -Blättern.
B-, C-, D-Spalten nach der zweiten Zeile Geben Sie das Speicherziel, den Speichernamen und die URL ein Es wird gespeichert, wenn Sie das Makro verschieben.
Übrigens können Sie das DOM auch mit driver.executescirpts usw. bearbeiten, sodass Sie die Webseite nach dem Spielen damit erfassen können.
schläfrig. .. .. Außerdem werde ich später ein Foto anhängen.
Recommended Posts