Selenium Tutorial VBA Excel (Chrome Web Scrap)ping)
โก Rezumat inteligent
Selenium VBA รฎn Excel automatizeazฤ Google Chrome pentru a extrage date din pagini web HTML, folosind Selenium bibliotecฤ de tipuri ศi metode WebDriver pentru a deschide un site, a localiza elementele tabelului dupฤ clasฤ ศi etichetฤ ศi a scrie rezultatele รฎn celulele foii de calcul.

Ce este Data Scraping Utilizarea Selenium?
Selenium este un instrument de automatizare care faciliteazฤ scraping informaศii din pagini web HTML, efectuarea de scanฤri webping implementate cu Google Chromeรn Excel, Selenium Biblioteca de tipuri permite VBA sฤ gestioneze un browser real รฎn locul motorului depreciat Internet Explorer.
Cum se pregฤteศte macrocomanda Excel รฎnainte de scanarea datelorping Utilizarea Selenium
Existฤ anumite cerinศe preliminare care trebuie รฎndeplinite asupra fiศierului macro Excel รฎnainte de a รฎncepe extragerea datelor.ping proces.
Aceste premise sunt dupฤ cum urmeazฤ:
Pas 1) Deschideศi o macrocomandฤ bazatฤ pe Excel ศi accesaศi opศiunea Dezvoltator din Excel.
Pas 2) Selectaศi opศiunea Visual Basic din panglica Dezvoltator.
Pas 3) Introduceศi un modul nou.
Pas 4) Iniศializaศi o nouฤ subrutinฤ ศi denumiศi-o test2.
Sub test2() End Sub
Rezultatul รฎn modul ar fi urmฤtorul:
Pas 5) Accesaศi opศiunea Referinศฤ din fila Instrumente ศi consultaศi Selenium Bibliotecฤ de tipuri. Aceastฤ bibliotecฤ ajutฤ la deschiderea Google Chrome ศi faciliteazฤ scriptarea macrocomenzilor.
Acum fiศierul Excel este gata sฤ interacศioneze cu browserul. Urmฤtorul pas este รฎncorporarea unui script macro care faciliteazฤ extragerea datelor.ping รฎn HTML.
Cum se deschide Google Chrome Folosind VBA
Iatฤ paศii pentru a deschide Google Chrome folosind VBA.
Pas 1) Declaraศi ศi iniศializaศi variabilele din subrutinฤ aศa cum se aratฤ mai jos.
Sub test2() Dim driver As New WebDriver Dim rowc, cc, columnC As Integer
Pas 2) A deschide Google Chrome folosind Selenium ศi VBA, scrieศi driver. Lansaศi โChromeโ ศi apฤsaศi F5.
Urmฤtorul ar fi codul.
Sub test2() Dim driver As New WebDriver Dim rowc, cc, columnC As Integer driver.Start "Chrome" Application.Wait Now + TimeValue("00:00:20") End Sub
Modulul ar rezulta astfel:
Cum sฤ deschizi un site web รฎn Google Chrome Folosind VBA
Odatฤ ce puteศi accesa Google Chrome folosind VBA, urmฤtorul pas este accesarea unui site web folosind VBA. Acest lucru este facilitat de funcศia Get, unde URL este transmisฤ ca ghilimele duble.
Modulul ar arฤta astfel:
Apฤsaศi F5 pentru a executa macrocomanda. Urmฤtoarea paginฤ web se va deschide รฎn Google Chrome aศa cum se aratฤ.
Sub test2() Dim driver As New WebDriver Dim rowc, cc, columnC As Integer driver.Start "Chrome" driver.Get "https://demo.guru99.com/test/web-table-element.php" Application.Wait Now + TimeValue("00:00:20") End Sub
Acum macrocomanda Excel este gata sฤ efectueze scanarea.ping sarcini. Urmฤtorul pas aratฤ cum pot fi exprimate informaศiiletracaplicat prin aplicarea Selenium ศi VBA.
Cum sฤ extragi informaศii de pe un site web folosind VBA
Sฤ presupunem cฤ traderul de o zi doreศte sฤ acceseze datele de pe site zilnic. De fiecare datฤ cรขnd traderul de o zi dฤ clic pe buton, acesta ar trebui sฤ extragฤ automat datele de piaศฤ รฎn Excel.
De pe site-ul web de mai sus, este necesar sฤ inspectaศi un element ศi sฤ observaศi cum sunt structurate datele. Accesaศi codul sursฤ HTML de mai jos apฤsรขnd Control + Shift +I.
<table class="datatable">
<thead>
<tr>
<th>Company</th>
<th>Group</th>
<th>Pre Close (Rs)</th>
<th>Current Price (Rs)</th>
<th>% Change</th>
</tr>
Dupฤ cum puteศi vedea, datele sunt structurate ca un singur tabel HTML. Prin urmare, pentru a extrage toate datele din tabelul HTML, trebuie sฤ proiectaศi o macrocomandฤ care extrage informaศiile din antet ศi datele corespunzฤtoare asociate tabelului. Efectuaศi sarcinile de mai jos.
Pas 1) Formulaศi o buclฤ For care parcurge informaศiile din antetul HTML ca o colecศie. Selenium Driverul localizeazฤ informaศiile din antet folosind metodele FindElementByClass() ศi FindElementByTag(), aศa cum se aratฤ.
Sub test2() Dim driver As New WebDriver Dim rowc, cc, columnC As Integer rowc = 2 Application.ScreenUpdating = False driver.Start "chrome" driver.Get "https://demo.guru99.com/test/web-table-element.php" For Each th In driver.FindElementByClass("dataTable").FindElementByTag("thead").FindElementsByTag("tr") cc = 1 For Each t In th.FindElementsByTag("th") Sheet2.Cells(1, cc).Value = t.Text cc = cc + 1 Next t Next th
Pas 2) Apoi, Selenium driverul localizeazฤ datele din tabel folosind o abordare similarฤ. Scrieศi urmฤtorul cod:
Sub test2() Dim driver As New WebDriver Dim rowc, cc, columnC As Integer rowc = 2 Application.ScreenUpdating = False driver.Start "chrome" driver.Get "https://demo.guru99.com/test/web-table-element.php" For Each th In driver.FindElementByClass("dataTable").FindElementByTag("thead").FindElementsByTag("tr") cc = 1 For Each t In th.FindElementsByTag("th") Sheet2.Cells(1, cc).Value = t.Text cc = cc + 1 Next t Next th For Each tr In driver.FindElementByClass("dataTable").FindElementByTag("tbody").FindElementsByTag("tr") columnC = 1 For Each td In tr.FindElementsByTag("td") Sheet2.Cells(rowc, columnC).Value = td.Text columnC = columnC + 1 Next td rowc = rowc + 1 Next tr Application.Wait Now + TimeValue("00:00:20") End Sub
Excel poate fi iniศializat folosind atributul Range sau atributul Cells al foii de calcul. Pentru a reduce complexitatea scriptului VBA, datele din colecศie sunt iniศializate cu atributul Cells al foii de calcul 2 din registrul de lucru. Atributul Text ajutฤ la plasarea textului sub o etichetฤ HTML.
Sub test2() Dim driver As New WebDriver Dim rowc, cc, columnC As Integer rowc = 2 Application.ScreenUpdating = False driver.Start "chrome" driver.Get "https://demo.guru99.com/test/web-table-element.php" For Each th In driver.FindElementByClass("dataTable").FindElementByTag("thead").FindElementsByTag("tr") cc = 1 For Each t In th.FindElementsByTag("th") Sheet2.Cells(1, cc).Value = t.Text cc = cc + 1 Next t Next th For Each tr In driver.FindElementByClass("dataTable").FindElementByTag("tbody").FindElementsByTag("tr") columnC = 1 For Each td In tr.FindElementsByTag("td") Sheet2.Cells(rowc, columnC).Value = td.Text columnC = columnC + 1 Next td rowc = rowc + 1 Next tr Application.Wait Now + TimeValue("00:00:20") End Sub
Modulul VBA ar arฤta astfel:
Pas 3) Dupฤ ce scriptul macro este gata, atribuiศi subrutina unui buton Excel ศi ieศiศi din modulul VBA. Etichetaศi butonul ca Refresh sau orice alt nume potrivit. Pentru acest exemplu, butonul este etichetat Refresh.
Pas 4) Apฤsaศi butonul Refresh pentru a obศine rezultatul de mai jos.
Pas 5) Comparaศi rezultatele din Excel cu rezultatele din Google Chrome.












