Škrabanie webu VBA Ako zošrotovať webové stránky pomocou programu Excel VBA?

Škrabanie webu v programe Excel VBA

VBA Web Scraping je technika prístupu na webové stránky a sťahovania údajov z tohto webu do našich počítačových súborov. Webové scraping je možné získať prístupom k externým aplikáciám, ako je Internet Explorer. Môžeme to urobiť dvoma spôsobmi, tj Early Binding & Late Binding.

Web Scraping s VBA znamená, že keď používame VBA na načítanie údajov z iných zdrojov na webe, môže to vyžadovať prihlásenie pre zdroje údajov, ale najskôr je potrebné povoliť odkazy zo sekcie nástrojov v editor VBA pre knižnicu Microsoft HTML, aby ste mali prístup na web z VBA.

Málokto z nás vie, že z programu Excel môžeme pristupovať na webové stránky a získavať z nich údaje. Áno, počuli ste to dobre. môžeme prehľadávať webové stránky, pristupovať k aplikáciám na prezeranie a mnoho ďalších. V tomto článku si ukážeme, ako podrobne napísať excelový kód VBA na škrabanie webu.

Spravidla otvárame webové stránky, kopírujeme údaje a vkladáme ich do našich súborov, ako sú napríklad Excel, Word alebo niektoré ďalšie súbory. Ale v tomto článku vám ukážeme, ako pristupovať na webové stránky z programu Excel a robiť mnoho ďalších vecí.

Ako zošrotovať dáta webových stránok pomocou VBA?

Túto šablónu VBA Web Scraping Excel si môžete stiahnuť tu - Šablóna VBA Web Scraping Excel

Ak chceme získať prístup k akýmkoľvek iným aplikáciám z programu Excel, môžeme to urobiť spôsobmi, napríklad „Early Binding“ a „Late Binding“. V štádiu začiatočníka je vždy bezpečné použiť techniku ​​„Early Binding“.

Na prístup na webovú stránku potrebujeme aplikácie na prezeranie, napríklad „ Internet Explorer “. Pretože sa jedná o externý objekt, je potrebné najskôr nastaviť referenciu.

Podľa nasledujúcich pokynov môžete webový odkaz odstrániť.

Krok 1: Definujte premennú VBA a typ údajov priraďte ako „ Internet Explorer “.

Kód:

 Sub Web_Scraping () Dim Internet_Explorer ako internet End Sub 

Ako vidíte vyššie, keď sa pokúšame nastaviť odkaz na Internet Explorer, nevidíme „Internet Explorer“, je to preto, že „Internet Explorer“ je externý objekt, takže je potrebné nastaviť odkaz.

Krok 2: Ak chcete nastaviť referenciu, choďte na „ Nástroje “ a zvoľte „ Referencie “.

V dolnom okne posuňte zobrazenie nadol a vyberte možnosť „ Microsoft Internet Controls “.

Krok 3: Začiarknite políčko „Microsoft Internet Controls“ a kliknite na OK. Teraz by sme mali vidieť tento názov objektu v zozname IntelliSense.

Kód:

 Sub Web_Scraping () Dim Internet_Explorer ako interný End Sub 

Krok 4: Vyberte „InternetExplorer“.

Kód:

 Sub Web_Scraping () Dim Internet_Explorer ako InternetExplorer End Sub 

Krok 5: Ďalej musíme nastaviť referenciu, aby ste povolili Internet Explorer. Pretože sa jedná o objektovú premennú, na nastavenie referencií je potrebné použiť kľúčové slovo „ Set “.

Kód:

 Sub Web_Scraping () Dim Internet_Explorer ako sada InternetExplorer Internet_Explorer = nový koncový sub InternetExplorer 

Krok 6: Teraz pomocou premennej „ Internet_Explorer “ môžeme používať vlastnosti a metódy internetového prieskumníka.

Enter the variable name and put a dot to see the IntelliSense list.

Code:

Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer. End Sub

Step 7: Now in order to view the internet explorer application, we need to choose “Visible” property and set the status as “True”.

Code:

 Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer.Visible = True End Sub 

Now run the code and you should see an Internet Explorer opens up on your computer.

Step 8: Because no web address has been mentioned we can see only a blank page. To give the web address to the internet explorer we need to “Navigation” method.

Code:

 Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate( End Sub 

Step 9: As you can see above “Navigation” method asking which URL to be navigated in internet explorer. Now I need to open the website “Wallstreetnmojo” and I can give the URL address as follows. “//www.wallstreetmojo.com/”

Code:

 Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate ("//www.wallstreetmojo.com") End Sub 

Now run the code, we should see the mentioned web address page in internet explorer.

Here we have a problem that once the web page is opened our code needs to wait until the page web page fully opened.

Step 10: We need to use the “Do While” loop in VBA to actually wait for our code to go any further until the mentioned page is fully loaded.

So, add below the “Do While” loop to force the macro to wait until the mentioned web page comes to the “Ready State Complete” mode.

Code:

 Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate ("//www.wallstreetmojo.com") Do While Internet_Explorer.ReadyState  READYSTATE_COMPLETE: Loop End Sub 

Step 11: Now let’s try to get information about the website in a single line. To get the information about the mentioned web address information we need to use the “Location Name” property.

Code:

 Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate ("//www.wallstreetmojo.com") Do While Internet_Explorer.ReadyState  READYSTATE_COMPLETE: Loop MsgBox Internet_Explorer.LocationName End Sub 

Run the code and in the message box, we would get the information about the website.

Step 12: Now at the bottom, we can also print website addresses as well.

Code:

 Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate ("//www.wallstreetmojo.com") Do While Internet_Explorer.ReadyState  READYSTATE_COMPLETE: Loop MsgBox Internet_Explorer.LocationName & vbNewLine & vbNewLine & Internet_Explorer.LocationURL End Sub 

Now this will tell about the website description and also shows the website address as well.

Things to Remember here

  • Web scraping is possible by accessing external applications like Internet Explorer.
  • We can do it in two ways i.e. Early Binding & Late Binding. With Early Binding, we can get to see the IntelliSense list but with late binding, we cannot get to see the IntelliSense list at all.