没有指定名称,ID或类别附加到数据的Excel Web抓取

时间:2020-09-05 20:31:21

标签: html excel vba web-scraping

我是Excel VBA编程的新手,我需要一些有关网络抓取的帮助。我目前正在尝试制定一些方法,以跟踪交货的当前状态并将其显示在Excel选项卡上。我正在尝试使用此网站https://webcsw.ocs.co.jp/csw/ECSWG0201R00003P.do,该网站仅在显示“空运提单号”时显示数据。输入。到目前为止,我只设法打开了Internet Explorer程序,输入了航空运单号,然后单击搜索按钮。

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://webcsw.ocs.co.jp/csw/ECSWG0201R00000P.do"
IE.Visible = True


While IE.busy
DoEvents
Wend

Set document = IE.document
With document
.getElementsByName("edtAirWayBillNo")(0).Value = ThisWorkbook.Sheets("Sheet3").Range("B2")
.getElementsByClassName("button btn_ex").Item.Click
End With

现在,当我在抓取数据时,找不到任何标志,例如名称,ID或类。我很麻烦从图表部分检索数据,这些数据都只用“ tbody”,“ tr”和“ td”标记。我尝试使用.getElementsByTagName方法,但是所有尝试都失败了。网站的html部分中,我需要从中检索数据,如下所示。请帮忙。

<table border="0" cellpadding="0" cellspacing="0" id="" style="border:#d0d0d0 1px dotted;" width="100%">
            <tbody id="chart_header">
                <tr>
                    <td rowspan="1" colspan="1" width="90px">Air WayBill No.</td>
                    <td rowspan="1" colspan="3" width="370px">Latest Tracking Record</td>
                    <td rowspan="1" colspan="1" width="150px">Shipper</td>
                    <td rowspan="1" colspan="1" width="150px">Receiver</td>
                    <td rowspan="1" colspan="1" width="40px">Pcs</td>
                    <td rowspan="1" colspan="1" width="80px">Actual Weight</td>
                    <td rowspan="1" colspan="1" width="70px">Vol. Weight</td>
                </tr>
            </tbody>

            <tbody id="chart" style="height: auto">
            <!-- record start -->
            
            
            
                 <tr>
                     <td>
                         <a href="#0" shape="rect">
                             25017894414
                         </a>
                     </td>
                    <td width="160px">
                         <div style=" position:relative; width:100%;align:left;vertical-align: 
                                      middle;">&nbsp;
                          <div style="position:absolute;top:0pt;left: 1pt; margin: 1px;">
                              Fri
                          </div>
                          <div style="position:absolute;top:0pt;left:25pt;">
                              04Sep2020
                          </div>
                          <div style="position:absolute;top:0pt;left:80pt;">
                              09:40
                          </div>
                         </div>
                     </td>
                     <td width="90px">
                         <input type="text" value="Product Scanned" style="width:90px;" tabindex="-1" class="readonly_left" readonly="readonly">
                     </td>
                     <td width="130px" style="border-width:1px 1px 1px 0px;">
                         
                             <img src="./image/tpStatus_BLUE4.gif" width="130px" height="16px" class="middle">
                         
                     </td>
                     <td>
                         <input type="text" value="SUZHOU/CHINA" style="width:145px;" tabindex="-1" class="readonly_left" readonly="readonly">
                     </td>
                     <td>
                        <input type="text" value="AICHI KEN/JAPAN" style="width:145px;" tabindex="-1" class="readonly_left" readonly="readonly">
                     </td>
                     <td class="t_right">
                         <input type="text" value="1" style="width:40px;" tabindex="-1" class="readonly_right" readonly="readonly">
                     </td>
                     <td class="t_right">
                         <input type="text" value="1.9kg" style="width:70px;" tabindex="-1" class="readonly_right" readonly="readonly">
                     </td>
                     <td class="t_right">
                         <input type="text" value="1.2kg" style="width:70px;" tabindex="-1" class="readonly_right" readonly="readonly">
                     </td>
                 </tr>
            
            
            <!-- record end -->
            </tbody>
        </table>

1 个答案:

答案 0 :(得分:0)

假设您等待结果加载,则应该可以使用ie.document.querySelector("#charttitle + table")来抓取表格,并使用剪贴板将该节点的outerHTML复制为表格以达到最佳效果。您可以循环执行,直到表格的结果超时(最好),或者使用显式等待。

#charttitle + table

是一个css选择器,用于查找表,该表是ID为charttitle的元素的相邻兄弟姐妹

'wait condition after click to submit 
Dim clipboard As Object

Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

clipboard.SetText ie.document.querySelector("#charttitle + table").outerHTML
clipboard.PutInClipboard
ActiveSheet.Cells(1, 1).PasteSpecial

您可以使用querySelectorAll和一个CSS通用同级组合器~

获取所有这些表。
Dim tables As Object, i As Long

Set tables = ie.document.querySelectorAll("#charttitle ~ table")

然后,您需要从For i = 0 to tables.length -1循环并使用tables.item(i).outerHTML访问循环中的当前表,并写出正确确定的所需输出行。

在此处了解有关CSS选择器的信息:

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

找到最后一行

https://www.rondebruin.nl/win/s9/win005.htm

请记住检查服务条款是否允许刮刮。