Excel VBA-网页搜集-HTML表格单元格的内部文本 [英] Excel VBA - Web Scraping - Inner Text of HTML Table Cell

查看:51
本文介绍了Excel VBA-网页搜集-HTML表格单元格的内部文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个宏,以根据货件号对Web货件的状态进行抓取.我正在使用XML-HTTP方法,但对VBA网络抓取是陌生的.我试图通过使用GetValuebyID,Tag,Class来获取值,但没有成功.

I am trying to build a macro to web scrape the status of a Cargo Shipment based on the shipment number. I am using the XML-HTTP method but I am new to VBA web scraping. I have tried to get the value by using the GetValuebyID,Tag, Class with no success.

突出显示的行是我需要从中提取值的那一行.[需要提取10个交付价值中的10个] [1]

The highlighted line is the one I need the value extracted from. [Need to Extract the 10 of 10 Delivered Value][1]

这是我对代码的了解.

Sub FlightStat()

Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim AllTables As IHTMLElementCollection
Dim MainTable As IHTMLTable


XMLReq.Open "GET", "https://www.unitedcargo.com/OurNetwork/TrackingCargo1512/Tracking.jsp?id=10205436&pfx=016", False

XMLReq.send

If XMLReq.Status <> 200 Then
    MsgBox "Problem" & vbNewLine & XMLReq.Status & " - " & XMLReq.statusText
    Exit Sub
End If

HTMLDoc.body.innerHTML = XMLReq.responseText

Set AllTables = HTMLDoc.getElementsByTagID("dispTable0")

  

End Sub

如果有人可以帮助我获得"10个交付中的10个",我将不胜感激.提取价值[1]: https://i.stack.imgur.com/xcOAZ.png

I would be grateful if someone could help me get the "10 of 10 Delivered" value extracted [1]: https://i.stack.imgur.com/xcOAZ.png

推荐答案

好吧,就像我在评论中写道.您可以使用IE抓取状态.

Ok, like I wrote in my comment. You can scrape the status with the IE.

请注意::如果无法加载动态内容,则以下代码没有内置超时.也不检查URL中传递的数字是否正确.

Please note: The following code has no timeout built in if the dynamic content cannot be loaded. There is also no check whether the number passed in the URL is correct.

Sub FlightStat()

Dim url As String
Dim ie As Object
Dim nodeTable As Object

  'You can handle the parameters id and pfx in a loop to scrape dynamic numbers
  url = "https://www.unitedcargo.com/OurNetwork/TrackingCargo1512/Tracking.jsp?id=10205436&pfx=016"

  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = False
  ie.navigate url
  Do Until ie.readyState = 4: DoEvents: Loop
  
  'Wait to load dynamic content after IE reports it's ready
  'We can do that in a loop to match the point the information is available
  Do
    On Error Resume Next
    Set nodeTable = ie.document.getElementByID("dispTable0")
    On Error GoTo 0
  Loop Until Not nodeTable Is Nothing
  
  'Get the status from the table
  MsgBox Trim(nodeTable.getElementsByTagName("li")(2).innertext)
  
  'Clean up
  ie.Quit
  Set ie = Nothing
  Set nodeTable = Nothing
End Sub

这篇关于Excel VBA-网页搜集-HTML表格单元格的内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆