Web scraping a table following a search loop

后端 未结 1 340
遥遥无期
遥遥无期 2021-01-26 04:33

The table\'s HTML is:

TITLE
1条回答
  •  离开以前
    2021-01-26 04:44

    Worked out an answer and feel silly for not getting it straight away:

    Dim tbls, tbl
    
        Set tbls = IE.Document.getElementsByTagName("TABLE")
    
        CR = Workbooks("My Book").Worksheets("My Sheet").Range("A" & RowCnt).Value
    
        For Each tbl In tbls
            If tbl.Rows(0).Cells(0).innertext = "TITLE" Then
                PCR = tbl.Rows(0).Cells(1).innertext
                If CR = PCR Then
                'my code inserted
                Exit For
                End If
            End If
        Next
    

    Really annoying that it was this simple and no idea how I was unable to search directly for Rows(0).Cells(1) with my If statement.

    Full code:

    Private Sub test()
    
    Dim IE As Object
    Dim RowCnt As Long
    Dim CIS, AN, CR As String
    
    RowCnt = 2
    
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    
    Do Until Workbooks("My Book").Worksheets("My Sheet").Range("A" & RowCnt).Value = ""
    
    CIS = Workbooks("My book").Worksheets("My sheet").Range("C" & RowCnt).Value
    
    IE.Navigate "First part" & CIS & "Second Part"
    
    While IE.Busy
    DoEvents
    Wend
    
    Dim tbls, tbl
    
        Set tbls = IE.Document.getElementsByTagName("TABLE")
    
        CR = Workbooks("My book").Worksheets("My sheet").Range("A" & RowCnt).Value
    
        For Each tbl In tbls
            If tbl.Rows(0).Cells(1).innertext = CR Then
                AN = tbl.Rows(1).Cells(1).innertext
                Exit For
            End If
        Next
    
    RowCnt = RowCnt + 1
    
    Loop
    
    
    End Sub
    

    0 讨论(0)
提交回复
热议问题