在搜索循环后网络抓取表格

在搜索循环后网络抓取表格

本文介绍了在搜索循环后网络抓取表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表的HTML是:

<TABLE   border="1" width="100%">

    <TR class="row0">
        <TD style="width: 30%"><strong>TITLE</strong></TD>
        <TD style="width: 40%">UNIQUE</TD>
        <TD style="width: 15%"><strong>BU Assigned</strong></TD>
        <TD style="width: 15%">REMOVED</TD>
    </TR>
    <TR class="row1">
        <TD style="width: 30%"><strong>Account Number</strong></TD>
        <TD style="width: 40%">TARGET INFORMATION</TD>
        <TD style="width: 15%"><strong>BU Logged</strong></TD>
        <TD style="width: 15%">REMOVED</TD>
    </TR>
    <TR class="row0">
        <TD style="width: 30%"><strong>3rd Party Reference</strong></TD>
        <TD style="width: 40%">REMOVED</TD>
        <TD style="width: 15%"><strong>Date Received</strong></TD>
        <TD style="width: 15%">REMOVED</TD>
    </TR>
    <TR class="row1">
        <TD style="width: 30%"><strong>Subject</strong></TD>
        <TD style="width: 40%">REMOVED</TD>
        <TD style="width: 15%"><strong>Date Logged</strong></TD>
        <TD style="width: 15%">REMOVED</TD>
    </TR>
    <TR class="row0">
        <TD style="width: 30%"><strong>Reason</strong></TD>
        <TD style="width: 40%">REMOVED</TD>
        <TD style="width: 15%"><strong>Last Action Date</strong></TD>
        <TD style="width: 15%">REMOVED</TD>
    </TR>
    <TR  class="row1" >
        <TD style="width: 30%"><strong>Status</strong></TD>
        <TD style="width: 40%">REMOVED</TD>
        <TD style="width: 15%"><strong>Date Resolved</strong></TD>
        <TD style="width: 15%">REMOVED</TD>
    </TR>

</TABLE>

我正在搜索页面中的所有表,试图找到"UNIQUE"(我认为它应该在Row(0).Cells(1)中)后拉出"Target INFORMATION",但这给了我运行时错误91-对象变量或未设置带块变量.

I am searching through all the tables in the page, trying to pull "Target INFORMATION" after locating "UNIQUE" which i assume should be in Row(0).Cells(1) but this gives me runtime error 91 - Object Variable or With block variable not set.

但是,当我搜索Row(0).Cells(0)为"TITLE"时,代码将找到该表,并且我可以愉快地进行.有人知道解决这个问题的方法吗?我曾尝试将单元号一直更改为10,以防万一我没有注意到一些空单元.注意:很可能有多个表,其中0,0具有相同的标题,唯一的唯一字段是从我的电子表格中获得的UNIQUE.

However when i search through for Row(0).Cells(0) being "TITLE" the code will find the table and i can proceed happily. Anyone know a way around this? I have tried changing the cell number all the way to 10 just in case there were some empty cells that I didn't notice. Note: there may well be multiple tables where 0,0 has the same title the only unique field is UNIQUE which is obtained from my spreadsheet.

推荐答案

得出一个答案,并因无法立即解决而感到愚蠢:

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

真的很烦,这很简单,也不知道我是如何无法使用If语句直接搜索Rows(0).Cells(1)的.

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.

完整代码:

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

这篇关于在搜索循环后网络抓取表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:26