Excel VBA tīmekļa nokasīšana
VBA Web Scraping ir metode, kā piekļūt tīmekļa lapām un lejupielādēt datus no šīs vietnes mūsu datora failos. Tīmekļa nokasīšana ir iespējama, piekļūstot ārējām lietojumprogrammām, piemēram, Internet Explorer. Mēs to varam izdarīt divos veidos, ti, agrīnā iesiešana un vēlā iesiešana.
Tīmekļa nokasīšana ar VBA nozīmē, ka, izmantojot VBA, lai iegūtu datus no citiem tīmekļa avotiem, tam var būt nepieciešami pieteikumi datu avotiem, taču vispirms, lai to izdarītu, mums ir jāiespējo atsauces no rīku sadaļas Microsoft HTML bibliotēkas VBA redaktoru, lai no VBA piekļūtu tīmeklim.
Ne daudzi no mums zina, ka, izmantojot Excel, mēs varam piekļūt tīmekļa lapām un iegūt datus no šīm tīmekļa lapām. Jā, jūs to dzirdējāt pareizi. mēs varam pārlūkot tīmekļa lapas, piekļūt pārlūkošanas lietojumprogrammām un daudz ko citu. Šajā rakstā mēs jums parādīsim, kā detalizēti uzrakstīt Excel VBA kodu tīmekļa nokasīšanai.
Parasti mēs atveram tīmekļa lapas, kopējam datus un ielīmējam tos savos failos, piemēram, Excel, Word vai citos failos. Bet šajā rakstā mēs jums parādīsim, kā piekļūt vietnēm no Excel un darīt daudz cita veida lietu.

Kā noņemt vietnes datus, izmantojot VBA?
Kad mēs vēlamies piekļūt citām Excel lietojumprogrammām, mēs to varam izdarīt, piemēram, “Early Binding” un “Late Binding”. Iesācēja posmā vienmēr ir droši izmantot “Early Binding” tehniku.
Lai piekļūtu vietnei, mums ir nepieciešamas pārlūkošanas programmas, piemēram, “ Internet Explorer ”. Tā kā tas ir ārējs objekts, mums vispirms ir jāiestata atsauce.
Veiciet tālāk norādītās darbības, lai noņemtu tīmekļa lūžņus.
1. darbība: definējiet mainīgo VBA un piešķiriet datu tipam “ Internet Explorer ”.
Kods:
Sub Web_Scraping () Dim Internet_Explorer kā interneta beigu apakšsadaļa

Kā redzat iepriekš, mēģinot iestatīt atsauci uz Internet Explorer, mēs neredzam “Internet Explorer”, tāpēc, ka “Internet Explorer” ir ārējs objekts, tāpēc mums ir jāiestata atsauce.
2. solis: Lai iestatītu atsauci, dodieties uz sadaļu “ Rīki ” un izvēlieties “ Atsauces ”.

Zemāk esošajā logā ritiniet uz leju un izvēlieties “ Microsoft Internet Controls ”.

3. darbība: atzīmējiet izvēles rūtiņu “Microsoft Internet Controls” un noklikšķiniet uz Labi. Tagad mums vajadzētu redzēt šo objekta nosaukumu IntelliSense sarakstā.
Kods:
Sub Web_Scraping () Dim Internet_Explorer Kā starpsadaļa Sub

4. darbība: izvēlieties “InternetExplorer”.
Kods:
Sub Web_Scraping () Dim Internet_Explorer Kā InternetExplorer End Sub

5. solis: Pēc tam mums jāiestata atsauce, lai iespējotu Internet Explorer. Tā kā tas ir objekta mainīgais, mums ir jāizmanto atslēgvārds “ Set ”, lai iestatītu atsauces.
Kods:
Sub Web_Scraping () Dim Internet_Explorer kā InternetExplorer iestatiet Internet_Explorer = Jauns InternetExplorer beigu apakšsadaļa

6. solis: Tagad, izmantojot mainīgo “ Internet_Explorer ”, mēs varam izmantot Internet Explorer īpašības un metodes.
Ievadiet mainīgā nosaukumu un ielieciet punktu, lai redzētu IntelliSense sarakstu.
Kods:
Apakš Web_Scraping () Dim Internet_Explorer kā InternetExplorer iestatiet Internet_Explorer = Jauns InternetExplorer Internet_Explorer. Beigu apakšnodaļa

7. solis: Tagad, lai skatītu Internet Explorer lietojumprogrammu, mums jāizvēlas rekvizīts “ Redzams ” un jāiestata statuss “ Patiesa ”.
Kods:
Sub Web_Scraping () Dim Internet_Explorer kā InternetExplorer iestatiet Internet_Explorer = Jauns InternetExplorer Internet_Explorer.Visible = True End Sub

Tagad palaidiet kodu, un jums vajadzētu redzēt, ka datorā tiek atvērts Internet Explorer .

8. solis: Tā kā nav pieminēta tīmekļa adrese, mēs varam redzēt tikai tukšu lapu. Lai tīmekļa adresi piešķirtu Internet Explorer, mums ir nepieciešama “ Navigācijas ” metode.
Kods:
Sub Web_Scraping () Dim Internet_Explorer kā InternetExplorer iestatiet Internet_Explorer = Jauns InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate (beigu apakšdaļa

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 kā InternetExplorer iestatiet Internet_Explorer = Jauns InternetExplorer Internet_Explorer.Visible = True Internet_Explorer.Navigate ("https://www.wallstreetmojo.com") Dari, kamēr Internet_Explorer.ReadyState & Internet_Explorer.LocationURL End Sub

Tagad tas pastāstīs par vietnes aprakstu un parādīs arī vietnes adresi.

Šeit jāatceras lietas
- Tīmekļa nokasīšana ir iespējama, piekļūstot ārējām lietojumprogrammām, piemēram, Internet Explorer.
- Mēs to varam izdarīt divos veidos, ti, agrīnā iesiešana un vēlā iesiešana. Izmantojot Early Binding, mēs varam redzēt IntelliSense sarakstu, bet ar novēlotu saistīšanu mēs vispār nevaram redzēt IntelliSense sarakstu.