Web Scraping


Microsoft HTML Object Library

Add the reference option under the tool tab and reference "Microsoft HTML Object Library" and "Microsoft Internet Control".

Dim ie As New SHDocVw.InternetExplorer 
Dim doc As New HTMLDocument
Dim ecoll As Object

ie.Visible = True
ie.navigate"http://bettersolutions.com/index.html"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE

Set doc = ie.document
Set ecoll = doc.getElementsByTagName("table")

Downloading Files - URLDownloadToFile

This uses the Windows API - urlmon.dll

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _ 
    Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
    ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub download_HK_picture()
   imgsrc = "https://bettersolutions.com/myimage.jpg"
   dlpath = "C:\DownloadedPics\"
   URLDownloadToFile 0, imgsrc, dlpath & "downloaded.jpg", 0, 0
End Sub

Using Selenium

Download the Selenium-type library (WebDriver) from the SeleniumHQ website: https://www.selenium.dev/.
Tools > References.
Look for "Selenium Type Library" or "WebDriver" and check the box to enable it. Click "OK" to close the references window.

Dim driver As New Selenium.ChromeDriver 
Dim elem As Selenium.WebElement

driver.Start "chrome", "https://example.com"
Set elem = driver.FindElementByXPath("//input[@id='search']")
elem.SendKeys "web scraping"

'do something

driver.Quit

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext