גירוד אתרים ב- VBA | כיצד לגרד אתרים באמצעות Excel VBA?

גירוד אינטרנט ב- Excel VBA

VBA Web Scraping היא טכניקה של גישה לדפי אינטרנט והורדת הנתונים מאותו אתר לקבצי המחשב שלנו. גירוד באינטרנט אפשרי על ידי גישה ליישומים חיצוניים כמו Internet Explorer. אנו יכולים לעשות זאת בשתי דרכים, כלומר כריכה מוקדמת וכריכה מאוחרת.

גירוד באינטרנט באמצעות VBA פירושו שכאשר אנו משתמשים ב- VBA כדי להביא את הנתונים ממקורות אחרים באינטרנט, יתכן וזה דורש כניסות למקורות הנתונים, אך ראשית, על מנת לעשות זאת, עלינו לאפשר את ההפניות ממקטע הכלים ב עורך ה- VBA עבור ספריית ה- HTML של מיקרוסופט על מנת לגשת לאינטרנט מ- VBA.

לא רבים מאיתנו יודעים שמ- Excel אנו יכולים לגשת לדפי האינטרנט ולקבל את הנתונים מדפי האינטרנט האלה. כן, שמעת את זה נכון. אנחנו יכולים לגרד בין דפי אינטרנט, לגשת ליישומי גלישה ועוד רבים אחרים. במאמר זה נראה לך כיצד לכתוב בפירוט קוד VBA מצטיין לגרידה באינטרנט.

בדרך כלל אנו פותחים את דפי האינטרנט, מעתיקים את הנתונים ומדביקים אותם בקבצים שלנו כמו Excel, Word או קבצים אחרים. אך במאמר זה נראה לכם כיצד לגשת לאתרים מ- Excel ולעשות דברים רבים אחרים.

כיצד לגרד נתוני אתרים באמצעות VBA?

אתה יכול להוריד תבנית Excel זו של גירוד אתרים ב- VBA כאן - תבנית Excel של גירוד אינטרנט של VBA

כאשר אנו רוצים לגשת ליישומים אחרים מ- Excel אנו יכולים לעשות זאת בדרכים כלומר "כריכה מוקדמת" ו"כריכה מאוחרת ". בשלב המתחיל, תמיד בטוח להשתמש בטכניקה "כריכה מוקדמת".

כדי לגשת לאתר אנו זקוקים ליישומי גלישה, למשל, " Internet Explorer ". מכיוון שמדובר באובייקט חיצוני עלינו לקבוע תחילה את ההפניה.

בצע את השלבים הבאים כדי לגרוטאות באינטרנט.

שלב 1: הגדר את המשתנה VBA והקצה את סוג הנתונים כ- " Internet Explorer ".

קוד:

 Sub Web_Scraping () עמום Internet_Explorer כ- Sub Internet End 

כפי שניתן לראות לעיל כאשר אנו מנסים להגדיר את ההפניה לאינטרנט אקספלורר איננו זוכים לראות את "אקספלורר", זאת מכיוון ש"אינטרנט אקספלורר "הוא אובייקט חיצוני ולכן עלינו להגדיר את ההפניה.

שלב 2: להגדרת ההפניה עבור אל " כלים " ובחר " הפניות ".

בחלון שלמטה גלול מטה ובחר " Microsoft Internet Controls ".

שלב 3: סמן את התיבה "בקרות אינטרנט של מיקרוסופט" ולחץ על אישור. כעת עלינו לראות את שם האובייקט הזה ברשימת IntelliSense.

קוד:

 תת Web_Scraping () עמום Internet_Explorer כ- Sub End Sub 

שלב 4: בחר "InternetExplorer".

קוד:

 Web WebScraping () עמעום Internet_Explorer כ- InternetExplorer Sub Sub 

שלב 5: לאחר מכן, עלינו להגדיר את ההפניה כדי לאפשר את Internet Explorer. מכיוון שמדובר במשתנה של אובייקט עלינו להשתמש במילת המפתח " Set " כדי להגדיר את ההפניות.

קוד:

 תת Web_Scraping () העמום את Internet_Explorer כ- InternetExplorer הגדר את Internet_Explorer = תת הסוף של InternetExplorer החדש 

שלב 6: כעת באמצעות המשתנה " Internet_Explorer " נוכל להשתמש במאפיינים ובשיטות של Internet Explorer.

הזן את שם המשתנה והניח נקודה כדי לראות את רשימת IntelliSense.

קוד:

תת Web_Scraping () עמעום Internet_Explorer כ- InternetExplorer הגדר Internet_Explorer = InternetExplorer Internet_Explorer חדש. סיום משנה

שלב 7: כעת על מנת להציג את היישום Explorer Internet, עלינו לבחור במאפיין " גלוי " ולהגדיר את הסטטוס כ"אמיתי ".

קוד:

 תת Web_Scraping () העמום את Internet_Explorer כ- InternetExplorer הגדר Internet_Explorer = InternetExplorer חדש Internet_Explorer.Visible = Sub True End 

כעת הפעל את הקוד ואתה אמור להופיע Internet Explorer במחשב שלך.

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.