VBA Web Scraping trong Excel
VBA Web Scraping là một kỹ thuật truy cập các trang web và tải dữ liệu từ trang web đó xuống các tệp máy tính của chúng tôi. Có thể tìm kiếm web bằng cách truy cập các ứng dụng bên ngoài như Internet Explorer. Chúng ta có thể làm điều đó theo hai cách tức là Ràng buộc sớm & Ràng buộc muộn.
Web Scraping với VBA có nghĩa là khi chúng tôi sử dụng VBA để tìm nạp dữ liệu từ các nguồn khác trên web, điều này có thể yêu cầu đăng nhập cho các nguồn dữ liệu, nhưng trước tiên, để làm như vậy, chúng tôi cần bật các tham chiếu từ phần công cụ trong trình soạn thảo VBA cho thư viện HTML của Microsoft để truy cập web từ VBA.
Không nhiều người trong chúng ta biết rằng từ excel, chúng ta có thể truy cập các trang web và lấy dữ liệu từ các trang web đó. Đúng, bạn nghe đúng đấy. chúng tôi có thể duyệt qua các trang web, truy cập các ứng dụng duyệt web và nhiều hơn nữa. Trong bài viết này, chúng tôi sẽ hướng dẫn bạn cách viết mã VBA trong excel cho web một cách chi tiết.
Thông thường, chúng tôi mở các trang web, sao chép dữ liệu và dán nó vào các tệp của mình như excel, word hoặc một số tệp khác. Nhưng trong bài viết này, chúng tôi sẽ hướng dẫn bạn cách truy cập trang web từ excel và thực hiện nhiều loại công cụ khác.

Làm thế nào để thu thập dữ liệu trang web bằng VBA?
Khi chúng tôi muốn truy cập bất kỳ ứng dụng nào khác từ excel, chúng tôi có thể thực hiện việc này theo các cách, tức là “Ràng buộc sớm” & “Ràng buộc muộn”. Ở giai đoạn mới bắt đầu, sử dụng kỹ thuật “Early Binding” luôn an toàn.
Để truy cập trang web, chúng tôi cần các ứng dụng duyệt web, chẳng hạn như “ Internet Explorer ”. Vì nó là một đối tượng bên ngoài, chúng ta cần đặt tham chiếu trước.
Làm theo các bước dưới đây để tìm mẩu tin lưu niệm trên web.
Bước 1: Xác định biến VBA và gán kiểu dữ liệu là “ Internet Explorer ”.
Mã:
Sub Web_Scraping () Dim Internet_Explorer As internet End Sub

Như bạn có thể thấy ở trên, khi chúng tôi cố gắng đặt tham chiếu cho Internet Explorer, chúng tôi không thấy “Internet Explorer”, điều này là do “Internet Explorer” là một đối tượng bên ngoài nên chúng tôi cần đặt tham chiếu.
Bước 2: Để đặt tham chiếu, hãy vào “ Công cụ ” và chọn “ Tham chiếu ”.

Trong cửa sổ bên dưới, cuộn xuống và chọn “ Microsoft Internet Controls ”.

Bước 3: Đánh dấu vào ô “Microsoft Internet Controls” và nhấp vào Ok. Bây giờ chúng ta sẽ thấy tên đối tượng này trong danh sách IntelliSense.
Mã:
Sub Web_Scraping () Dim Internet_Explorer As inter End Sub

Bước 4: Chọn “InternetExplorer”.
Mã:
Sub Web_Scraping () Dim Internet_Explorer As InternetExplorer End Sub

Bước 5: Tiếp theo, chúng ta cần thiết lập tham chiếu để kích hoạt Internet Explorer. Vì đây là một biến đối tượng, chúng ta cần sử dụng từ khóa " Set " để đặt các tham chiếu.
Mã:
Sub Web_Scraping () Dim Internet_Explorer As InternetExplorer Đặt Internet_Explorer = New InternetExplorer End Sub

Bước 6: Bây giờ, bằng cách sử dụng biến “ Internet_Explorer ” , chúng ta có thể sử dụng các thuộc tính và phương thức của internet explorer.
Nhập tên biến và đặt một dấu chấm để xem danh sách IntelliSense.
Mã:
Sub Web_Scraping () Dim Internet_Explorer As InternetExplorer Đặt Internet_Explorer = InternetExplorer Internet_Explorer mới. Kết thúc Sub

Bước 7: Bây giờ, để xem ứng dụng Internet explorer, chúng ta cần chọn thuộc tính “ Hiển thị ” và đặt trạng thái là “ Đúng ”.
Mã:
Sub Web_Scraping () Dim Internet_Explorer As InternetExplorer Đặt Internet_Explorer = New InternetExplorer Internet_Explorer.Vbroken = True End Sub

Bây giờ hãy chạy mã và bạn sẽ thấy một Internet Explorer mở ra trên máy tính của bạn.

Bước 8: Vì không có địa chỉ web nào được đề cập nên chúng ta chỉ có thể thấy một trang trống. Để cung cấp địa chỉ web cho trình khám phá internet, chúng ta cần sử dụng phương pháp " Điều hướng ".
Mã:
Sub Web_Scraping () Dim Internet_Explorer As InternetExplorer Đặt Internet_Explorer = New InternetExplorer Internet_Explorer.Vosystem = 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 Đặt Internet_Explorer = New InternetExplorer Internet_Explorer.Vosystem = True Internet_Explorer.Navigate ("https://www.wallstreetmojo.com") Do While Internet_Explorer.ReadyState READYSTATE_COMPLine & vNewpgine Internet vNewpame & Internet_Explorer.LocationURL End Sub

Bây giờ điều này sẽ cho biết về mô tả trang web và cũng hiển thị địa chỉ trang web.

Những điều cần nhớ ở đây
- Có thể tìm kiếm web bằng cách truy cập các ứng dụng bên ngoài như Internet Explorer.
- Chúng ta có thể làm điều đó theo hai cách, tức là Ràng buộc sớm & Ràng buộc muộn. Với Liên kết sớm, chúng ta có thể xem danh sách IntelliSense, nhưng với ràng buộc muộn, chúng ta không thể xem được danh sách IntelliSense.