VBA 웹 스크래핑 | Excel VBA를 사용하여 웹 사이트를 스크랩하는 방법은 무엇입니까?

Excel VBA 웹 스크랩 핑

VBA 웹 스크래핑 은 웹 페이지에 액세스하고 해당 웹 사이트의 데이터를 컴퓨터 파일로 다운로드하는 기술입니다. 웹 스크래핑은 Internet Explorer와 같은 외부 응용 프로그램에 액세스하여 가능합니다. Early Binding과 Late Binding의 두 가지 방법으로 할 수 있습니다.

VBA를 사용한 웹 스크래핑은 VBA를 사용하여 웹의 다른 소스에서 데이터를 가져올 때 데이터 소스에 대한 로그인이 필요할 수 있지만 먼저 이렇게하려면의 도구 섹션에서 참조를 활성화해야합니다. VBA에서 웹에 액세스하기위한 Microsoft HTML 라이브러리 용 VBA 편집기.

Excel에서 웹 페이지에 액세스하고 해당 웹 페이지에서 데이터를 가져올 수 있다는 사실을 아는 사람은 많지 않습니다. 네, 잘 들었습니다. 웹 페이지를 훑어보고 브라우징 애플리케이션에 액세스하는 등의 작업을 할 수 있습니다. 이 기사에서는 웹 스크래핑을위한 Excel VBA 코드를 작성하는 방법을 자세히 설명합니다.

일반적으로 웹 페이지를 열고 데이터를 복사 한 다음 Excel, Word 또는 기타 파일과 같은 파일에 붙여 넣습니다. 그러나이 기사에서는 Excel에서 웹 사이트에 액세스하는 방법과 다른 많은 작업을 수행하는 방법을 보여줍니다.

VBA를 사용하여 웹 사이트 데이터를 스크랩하는 방법?

이 VBA 웹 스크래핑 Excel 템플릿을 여기에서 다운로드 할 수 있습니다 – VBA 웹 스크래핑 Excel 템플릿

Excel에서 다른 응용 프로그램에 액세스하려는 경우 "Early Binding"및 "Late Binding"과 같은 방식으로이를 수행 할 수 있습니다. 초보자 단계에서는 "Early Binding"기술을 사용하는 것이 항상 안전합니다.

웹 사이트에 액세스하려면 " Internet Explorer " 와 같은 검색 애플리케이션이 필요합니다 . 외부 객체이기 때문에 먼저 참조를 설정해야합니다.

웹 스크랩을하려면 아래 단계를 따르세요.

1 단계 : VBA 변수를 정의하고 데이터 유형을 " Internet Explorer " 로 지정합니다 .

암호:

 Sub Web_Scraping () Dim Internet_Explorer As internet End Sub 

위에서 볼 수 있듯이 Internet Explorer에 대한 참조를 설정하려고 할 때 "Internet Explorer"가 표시되지 않습니다. 이는 "Internet Explorer"가 외부 개체이므로 참조를 설정해야하기 때문입니다.

2 단계 : 참조를 설정하려면 " 도구 " 로 이동하여 " 참조 "를 선택합니다 .

아래 창에서 아래로 스크롤하여“ Microsoft Internet Controls ”를 선택합니다 .

3 단계 : "Microsoft Internet Controls"상자를 선택하고 확인을 클릭합니다. 이제 IntelliSense 목록에서이 개체 이름을 볼 수 있습니다.

암호:

 Sub Web_Scraping () Dim Internet_Explorer As inter End Sub 

4 단계 : "InternetExplorer"를 선택합니다.

암호:

 Sub Web_Scraping () Dim Internet_Explorer As InternetExplorer End Sub 

5 단계 : 다음으로 Internet Explorer를 활성화하기위한 참조를 설정해야합니다. 이것은 객체 변수이기 때문에 참조를 설정 하기 위해“ Set ”키워드를 사용해야합니다 .

암호:

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

6 단계 : 이제“ Internet_Explorer ” 변수를 사용하여 Internet explorer의 속성과 메서드를 사용할 수 있습니다.

변수 이름을 입력하고 점을 입력하면 IntelliSense 목록이 표시됩니다.

암호:

Sub Web_Scraping () Dim Internet_Explorer As InternetExplorer Set Internet_Explorer = 새 InternetExplorer Internet_Explorer. End Sub

7 단계 : 이제 인터넷 익스플로러 애플리케이션을 보려면“ Visible ”속성 을 선택 하고 상태를“ True ” 로 설정해야합니다 .

암호:

 Sub Web_Scraping () Dim Internet_Explorer를 InternetExplorer로 설정 Internet_Explorer = 새 InternetExplorer Internet_Explorer.Visible = True End Sub 

이제 코드를 실행하면 컴퓨터에서 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.