VBA žiniatinklio grandymas - Kaip atsisakyti svetainių naudojant „Excel VBA“?

„Excel VBA“ žiniatinklio grandymas

„VBA Web Scraping“ yra būdas patekti į tinklalapius ir atsisiųsti duomenis iš tos svetainės į mūsų kompiuterio failus. Žiniatinklis gali būti nuskaitytas pasiekiant išorines programas, pvz., „Internet Explorer“. Mes galime tai padaryti dviem būdais, ty ankstyvuoju ir vėlyvuoju įrišimu.

Žiniatinklio nuskaitymas naudojant VBA reiškia, kai mes naudojame VBA duomenims gauti iš kitų žiniatinklio šaltinių, tam gali reikėti prisijungti prie duomenų šaltinių, tačiau pirmiausia, norėdami tai padaryti, turime įgalinti nuorodas iš įrankių skyriaus. „Microsoft HTML“ bibliotekos VBA redaktorius, kad iš VBA galėtumėte pasiekti internetą.

Nedaugelis iš mūsų žino, kad iš „Excel“ galime pasiekti tinklalapius ir gauti duomenis iš tų tinklalapių. Taip, tu teisingai girdėjai. galime naršyti tinklalapius, pasiekti naršymo programas ir daug daugiau. Šiame straipsnyje mes jums parodysime, kaip išsamiai parašyti „Excel“ VBA kodą žiniatinklio grandymui.

Paprastai mes atidarome tinklalapius, nukopijuojame duomenis ir įklijuojame juos į savo failus, pvz., „Excel“, „Word“ ar kitus failus. Tačiau šiame straipsnyje mes parodysime, kaip iš „Excel“ pasiekti svetaines ir atlikti daugybę kitų dalykų.

Kaip atsisakyti svetainės duomenų, naudojant VBA?

Kai norime pasiekti „Excel“ bet kokias kitas programas, tai galime padaryti būdais, ty „Ankstyvas įrišimas“ ir „Vėlyvas įrišimas“. Pradedančiųjų etape visada saugu naudoti „Early Binding“ techniką.

Norėdami patekti į svetainę, turime naršyti programas, pvz., „ Internet Explorer “. Kadangi tai yra išorinis objektas, pirmiausia turime nustatyti nuorodą.

Atlikite toliau nurodytus veiksmus, kad pašalintumėte žiniatinklį.

1 veiksmas: apibrėžkite VBA kintamąjį ir priskirkite duomenų tipą kaip „ Internet Explorer “.

Kodas:

Sub Web_Scraping () Dim Internet_Explorer kaip interneto pabaigos sub

Kaip matote aukščiau, bandydami nustatyti nuorodą į „Internet Explorer“, nematome „Internet Explorer“, nes „Internet Explorer“ yra išorinis objektas, todėl turime nustatyti nuorodą.

2 žingsnis: Norėdami nustatyti nuorodą, eikite į „ Įrankiai “ ir pasirinkite „ Nuorodos “.

Žemiau esančiame lange slinkite žemyn ir pasirinkite „ Microsoft Internet Controls “.

3 žingsnis: Pažymėkite langelį „Microsoft Internet Controls“ ir spustelėkite Gerai. Dabar turėtume pamatyti šį objekto pavadinimą „IntelliSense“ sąraše.

Kodas:

Sub Web_Scraping () Dim Internet_Explorer As inter End Sub

4 žingsnis: Pasirinkite „InternetExplorer“.

Kodas:

Sub Web_Scraping () Dim Internet_Explorer Kaip InternetExplorer End Sub

5 žingsnis: Tada turime nustatyti nuorodą, kad įgalintumėte „Internet Explorer“. Kadangi tai yra objekto kintamasis, nuorodoms nustatyti turime naudoti raktinį žodį „ Set “.

Kodas:

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

6 žingsnis: Dabar, naudodami kintamąjį „ Internet_Explorer “, galime naudoti „Internet Explorer“ ypatybes ir metodus.

Įveskite kintamojo pavadinimą ir įdėkite tašką, kad pamatytumėte „IntelliSense“ sąrašą.

Kodas:

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

7 žingsnis: Dabar, norėdami peržiūrėti „Internet Explorer“ programą, turime pasirinkti „ Visible “ ypatybę ir nustatyti būseną kaip „ True “.

Kodas:

Sub Web_Scraping () Dim Internet_Explorer, kaip InternetExplorer, nustatykite Internet_Explorer = Naujas InternetExplorer Internet_Explorer.Visible = True End Sub

Dabar paleiskite kodą ir turėtumėte pamatyti, kad jūsų kompiuteryje atsidaro „ Internet Explorer“ .

8 žingsnis: Kadangi nebuvo paminėtas nė vienas interneto adresas, galime matyti tik tuščią puslapį. Norėdami suteikti interneto adresą interneto naršyklei, turime naudoti „ Naršymo “ metodą.

Kodas:

Sub Web_Scraping () pritemdyti Internet_Explorer kaip InternetExplorer Nustatyti Internet_Explorer = Naujas 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. “https://www.wallstreetmojo.com/”

Code:

Sub Web_Scraping() Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = New InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate ("https://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 ("https://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 ("https://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 ("https://www.wallstreetmojo.com") Darykite, kol interneto_Explorer.ReadyState & Internet_Explorer.LocationURL End Sub

Dabar tai pasakys apie svetainės aprašymą ir taip pat parodys svetainės adresą.

Ką čia reikia atsiminti

  • Žiniatinklis gali būti nuskaitytas pasiekiant išorines programas, pvz., „Internet Explorer“.
  • Tai galime padaryti dviem būdais, ty ankstyvuoju ir vėlyvuoju įrišimu. Su „Early Binding“ mes galime pamatyti „IntelliSense“ sąrašą, tačiau su vėlyvu įrišimu mes negalime pamatyti „IntelliSense“ sąrašo apskritai.

Įdomios straipsniai...