VBA HTML Listing Info Pull

后端 未结 3 693
终归单人心
终归单人心 2021-01-26 14:29

I am looking to follow a series of URL\'s that are found in column A (example: https://www.ebay.com/itm/Apple-iPhone-7-GSM-Unlocked-Verizon-AT-T-TMobile-Sprint-32GB-128GB-256GB/

3条回答
  •  耶瑟儿~
    2021-01-26 15:12

    There are a lot of things to fix in your code. It is late here so I will just give pointers (and update fully later) and working code below:

    1. Declare all variables and use appropriate type
    2. Review For Loops and how transpose can be used to create a 1d array of urls pulled from sheet to loop over
    3. Review the difference between querySelector and querySelectorAll methods
    4. Review CSS selectors (you are specifying everything as type selector when in fact you are not selecting by tag for the elements of interest; nor by your stated text)
    5. Think about placement of your IE object creation and of your .Navigate2 to make use of existing object
    6. Make sure to use distinct loop counters
    7. Be sure not to overwrite values in sheet

    Code:

    Option Explicit
    Public Sub ListingInfo()
        Dim ie As New InternetExplorer, ws As Worksheet
        Dim i As Long, urls(), rowCounter As Long
        Dim title As Object, price As Object, description As Object
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        urls = Application.Transpose(ws.Range("A1:A2").Value) '<= Adjust
        With ie
            .Visible = True
            For i = LBound(urls) To UBound(urls)
                If InStr(urls(i), "http") > 0 Then
                    rowCounter = rowCounter + 1
                    .Navigate2 urls(i)
                    While .Busy Or .readyState < 4: DoEvents: Wend
                    Set title = .document.querySelector(".it-ttl")
                    Set price = .document.querySelector("#prcIsum")
                    Set description = .document.querySelector("#viTabs_0_is")
    
                    ws.Cells(rowCounter, 3) = title.innerText
                    ws.Cells(rowCounter, 4) = price.innerText
                    ws.Cells(rowCounter, 5) = description.innerText
                    Set title = Nothing: Set price = Nothing: Set description = Nothing
                End If
            Next
            .Quit
        End With
    End Sub
    

提交回复
热议问题