Program/기타
엑셀 크롤링 - 쿠팡에서 가격 가지고 오기
키클쥔장
2024. 2. 22. 17:19
정말 오랜만에 구경한 엑셀 vb 스크립트 ;;
아는사람이 물어본게 있어서 후다닥 검색해서 하나 만들어봤다.
예제는 쿠팡 상품페이지에서 가격 가지고 오기
# total_price라는 클래스를 찾아서 Text 값을 읽어온다.
Private Sub CommandButton1_Click()
Dim IE As Object
Dim elements As Object
Dim element As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.navigate ("https://www.coupang.com/vp/products/77736293")
.Visible = False
End With
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set elements = IE.document.getElementsByClassName("total-price")
For Each element In elements
Debug.Print element.innerText
Next element
IE.Quit
Set IE = Nothing
End Sub
두번째 방법은 크롬에서 selector를 이용해서 값을 구하는방법
크롬에서 F12 클릭 후 원하는 위치에서 마우스 오른쪽버튼 -> 검사 하면 요소에 위치로 이동하는데
이동된 위치에서 마우스 오른쪽 > 복사 > selector 복사를 누르면 해당 위치가 복사된다.
Private Sub CommandButton1_Click()
Dim IE As Object
Dim element As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.navigate ("https://www.coupang.com/vp/products/6714200405")
.Visible = False
End With
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
' 요소를 찾기 위해 태그 이름과 클래스 이름을 조합하여 사용
Set element = IE.document.querySelector("#contents > div.prod-atf > div.prod-atf-main > div.prod-buy.new-oos-style.not-loyalty-member.eligible-address.without-subscribe-buy-type.DISPLAY_0.only-one-delivery.with-seller-store-rating > div.prod-price-container > div.prod-price > div > div.prod-coupon-price.price-align.major-price-coupon > span > strong")
' 개체가 null인지 확인
If Not element Is Nothing Then
MsgBox element.innerText
Else
MsgBox "요소를 찾을 수 없습니다."
End If
IE.Quit
Set IE = Nothing
End Sub
구한 경로를 Set element = IE.document.querySelector(" 위에서 구한 selector 경로 ") 에 넣고 실행
테스트로 만든 엑셀파일도 첨부한다.