从Web抓取到Excel时复制数据时出错

从Web抓取到Excel时复制数据时出错,excel,vba,web-scraping,Excel,Vba,Web Scraping,我正在寻找您的帮助,我的代码拉数据从网络到Excel 要获取数据,请访问Web: 获取数据的步骤: 把“Cát Lái”放到“Khu v”字段ự吉奥新罕布什尔州ận集装箱(选择海港) 将容器编号放入“容器”字段 取消选择“Chỉ 吴绿翠ể氮铜ối“显示数据表中的所有行 单击“搜索”以显示用于搜索容器信息的数据表-结果 问题: Excel中每行从web到Excel的数据(分别为 找到的每个容器编号)似乎与前一个相同 结果,而此容器编号的信息可以为空。对于 示例:事件时间2“10/4/2020

我正在寻找您的帮助,我的代码拉数据从网络到Excel

要获取数据,请访问Web:

  • 获取数据的步骤:
把“Cát Lái”放到“Khu v”字段ự吉奥新罕布什尔州ận集装箱(选择海港)

将容器编号放入“容器”字段

取消选择“Chỉ 吴绿翠ể氮铜ối“显示数据表中的所有行

单击“搜索”以显示用于搜索容器信息的数据表-结果

问题: Excel中每行从web到Excel的数据(分别为 找到的每个容器编号)似乎与前一个相同 结果,而此容器编号的信息可以为空。对于 示例:事件时间2“10/4/2020 3:07:00 PM”在以下时间重复 容器“TEMU331130”,而此容器没有事件时间 二,

希望你能给我一些建议来解决这个重复的问题。附件Excel文件供您参考。谢谢

Sub PullDataFromWeb()
  Dim IE As Object, W As Excel.Worksheet
  Dim doc As HTMLDocument
  Dim lastRow As Integer, b As Boolean, tmp As String
  Dim lis, li
  Set W = ThisWorkbook.Sheets("Sheet1")
  Set IE = VBA.CreateObject("InternetExplorer.Application")
  IE.Visible = True   'hien cua so IE
  IE.navigate "https://eport.saigonnewport.com.vn/Pages/Common/Containers_new"
  Do While IE.Busy Or IE.readyState <> 4      'doi IE chay xong
    Application.Wait DateAdd("s", 1, Now)
  Loop
  Set doc = IE.document

  lastRow = W.Range("B" & W.UsedRange.Rows.Count + 2).End(xlUp).Row        'dong cuoi cung trong cot B container
  If lastRow < 2 Then GoTo Ends
  On Error Resume Next
  For intRow = 2 To lastRow     'tu dong toi dong
    b = False
    b = W.Range("I" & intRow).Value Like "[Yy]"
    If W.Range("B" & intRow).Value <> "" And Not b Then
      doc.getElementById("txtItemNo_I").Value = W.Range("B" & intRow).Value 'so cont
      doc.getElementById("cbSite_VI").Value = W.Range("A" & intRow).Value
      doc.getElementById("chkInYard_I").Checked = False
      doc.getElementById("ContentPlaceHolder2_btnSearch").Click 'click Search
      '----------------------------------------------
      Do While IE.Busy Or IE.readyState <> 4
        Application.Wait DateAdd("s", 1, Now)
      Loop
      '----------------------------------------------
      strFindContainer = doc.getElementById("ContentPlaceHolder2_lblNotice").innerText
      W.Range("H" & intRow) = strFindContainer
      If strFindContainer Like "T*m th*y * container*" Then
        strEventtime1 = doc.getElementById("grdContainer_DXDataRow0").Cells(0).innerText
        strEventtype1 = doc.getElementById("grdContainer_DXDataRow0").Cells(1).innerText
        strLocation1 = doc.getElementById("grdContainer_DXDataRow0").Cells(2).innerText
        strEventtime2 = doc.getElementById("grdContainer_DXDataRow1").Cells(0).innerText
        strEventtype2 = doc.getElementById("grdContainer_DXDataRow1").Cells(1).innerText
        W.Range("C" & intRow) _
          .Resize(, 5).Value = Array(strEventtime1, strEventtype1, strLocation1, _
                         strEventtime2, strEventtype2)
      End If
    End If
  Next
Ends:
  IE.Quit
  Set IE = Nothing    'Cleaning up
  Set objElement = Nothing
  Set objCollection = Nothing
  Application.StatusBar = ""
  Application.DisplayAlerts = True
End Sub
Sub-PullDataFromWeb()
尺寸IE作为对象,W作为Excel.工作表
作为HTMLDocument的Dim doc
Dim lastRow为整数,b为布尔值,tmp为字符串
李,李
Set W=此工作簿。工作表(“工作表1”)
设置IE=VBA.CreateObject(“InternetExplorer.Application”)
可见=真的
即“导航”https://eport.saigonnewport.com.vn/Pages/Common/Containers_new"
当你忙的时候或准备好的时候做4'doi IE chay xon
Application.Wait DateAdd(“s”,1,Now)
环
Set doc=IE.document
lastRow=W.Range(“B”和W.UsedRange.Rows.Count+2)。结束(xlUp)。行“dong cuoi cung trong cot B”容器
如果lastRow<2,则转到结束
出错时继续下一步
对于最后一行“tu dong toi dong”的介绍=2
b=错误
b=W.Range(“I”和intRow)。值类似于“[Yy]”
如果W.Range(“B”和intRow).Value“”而不是B,则
doc.getElementById(“txtItemNo_I”)。Value=W.Range(“B”和intRow)。Value'so cont
doc.getElementById(“cbSite_VI”).Value=W.Range(“A”和intRow).Value
doc.getElementById(“chkInYard_I”).Checked=False
doc.getElementById(“ContentPlaceholder 2\u btnSearch”)。单击“单击搜索”
'----------------------------------------------
在忙或准备状态4时执行
Application.Wait DateAdd(“s”,1,Now)
环
'----------------------------------------------
strFindContainer=doc.getElementById(“ContentPlaceHolder2\u lblNotice”).innerText
W.Range(“H”和intRow)=strFindContainer
如果strFindContainer像“T*m th*y*container*”那么
strEventtime1=doc.getElementById(“grdContainer_DXDataRow0”)。单元格(0)。innerText
strEventtype1=doc.getElementById(“grdContainer_DXDataRow0”)。单元格(1)。innerText
strLocation1=doc.getElementById(“grdContainer_DXDataRow0”)。单元格(2)。innerText
strEventtime2=doc.getElementById(“grdContainer_DXDataRow1”)。单元格(0)。innerText
strEventtype2=doc.getElementById(“grdContainer_DXDataRow1”)。单元格(1)。innerText
W.范围(“C”和介绍)_
.Resize(,5).Value=数组(strEventtime1,strEventtype1,strLocation1_
strEventtime2,strEventtype2)
如果结束
如果结束
下一个
末端:
即退出
设置IE=Nothing'清理
设置对象=无
Set objCollection=Nothing
Application.StatusBar=“”
Application.DisplayAlerts=True
端接头

在最后一个
下一个
之前,请确保将所有相关字符串变量分配给
vbNullstring
数组中的变量(strEventtime1、strEventtype1、strLocation1、strEventtime2、strEventtype2)
因为它位于
If
中,所以当
If
未满足时,先前的值将保留在以后的循环迭代中