【问题标题】:Web scraping not finding any objects网页抓取未找到任何对象
【发布时间】:2021-09-01 04:22:36
【问题描述】:

我正在尝试将内部网页上的表格内容保存到 Excel 表格中,但我无法抓取任何 HTML 元素。 我尝试通过 ID、类名和标记名获取所有元素,但无济于事。有人可以告诉我我做错了什么吗?

Sub Shuffle()

Application.DisplayAlerts = False
'On Error GoTo ErrHandler
Dim SheetName, FavNumber As String


'LTI
SheetName = "LTI"
FavNumber = "2282804"
'Call IE_Sledgehammer


   Dim ie As InternetExplorerMedium, I As Long, strText As String
   Dim doc As Object, hTable As Object, hBody As Object, hTR As Object, hTD As Object
   Dim tb As Object, bb As Object, tr As Object
   Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet



   Set wb = Excel.ActiveWorkbook
   'Set ws = wb.ActiveSheet
   
   
'LastRow = Sheets(SheetName).Cells(Sheets(SheetName).Rows.Count, "A").End(xlUp).Row
'If LastRow > 1 Then
'    Sheets(SheetName).Range("A2:AA" & LastRow + 1).ClearContents
'End If
'Application.Wait (Now + TimeValue("0:00:02"))

Set ie = Nothing
Application.Wait (Now + TimeValue("0:00:02"))
Set ie = New InternetExplorerMedium
ie.Visible = True


 ie.navigate "https://synergi.de-prod.dk/synergi/favourite/" & FavNumber
 On Error Resume Next
 Do While ie.Busy: DoEvents: Loop
 Do While ie.readyState <> 4:
 

 Set doc = ie.document
 
 Dim td As Object
 Set td = doc.getElementsByTagName("td")
 Set Post6a = doc.getElementsByTagName("th")(0).outerText: MsgBox Post6a
 Set Post6b = doc.getElementsByTagName("th")(0).innerText: MsgBox Post6b
 Set Post6c = doc.getElementsByTagName("td"): MsgBox Post6c.Length
 
 Dim th As Object
 Set th = doc.getElementsByTagName("th")
 Set Post8a = doc.getElementsByTagName("td")(0).outerText: MsgBox Post8a
 Set Post8b = doc.getElementsByTagName("td")(0).innerText: MsgBox Post8b
 Set Post8c = doc.getElementsByTagName("th"): MsgBox Post8c.Length
 
 Dim span As Object
 Set span = doc.getElementsByTagName("span")
 Set Post7a = doc.getElementsByTagName("span")(0).outerText: MsgBox Post7a
 Set Post7b = doc.getElementsByTagName("span")(0).innerText: MsgBox Post7b
 Set Post7c = doc.getElementsByTagName("span"): MsgBox Post7c.Length

 Dim div As Object
 Set span = doc.getElementsByTagName("div")
 Set Post9a = doc.getElementsByTagName("div")(0).outerText: MsgBox Post9a
 Set Post9b = doc.getElementsByTagName("div")(0).innerText: MsgBox Post9b
 Set Post9c = doc.getElementsByTagName("div"): MsgBox Post9c.Length
 
     
  y = 1   'Column A in Excel
  z = 2   'Row 1 in Excel
  
Set hTable = doc.getElementsByTagName("table")
 
 For Each tb In hTable

    Set hBody = tb.getElementsByTagName("tbody")
    For Each bb In hBody

        Set hTR = bb.getElementsByTagName("tr")
        For Each tr In hTR


             Set hTD = tr.getElementsByTagName("td")
             y = 1 ' Resets back to column A
             For Each td In hTD
               wb.Sheets(SheetName).Cells(z, y).Value = td.innerText
               y = y + 1
               'MsgBox td.innerText
             Next td
             DoEvents
             z = z + 1
        Next tr
        Exit For
    Next bb
Exit For
  Next tb

Application.Wait (Now + TimeValue("0:00:02"))
'IE.Quit
ie.Quit
End Sub

网页的 HTML 代码如下:

【问题讨论】:

    标签: html internet-explorer


    【解决方案1】:

    如果需要将网站中的表格内容提取到Excel中,可以试试这两种方法:

    1. 使用 IE:数据在 iframe 中,需要协商

    2. 使用 XMLHTTP 请求 - 更快且无需打开浏览器。它使用 iframe 文档 URL 的第一部分,这是 iframe 导航到的内容。

    更多细节可以参考下面的案例,里面有一个简单的例子: Extract table from webpage using VBA

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 2017-09-20
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多