【问题标题】:Web Scraping by VBA for JavaScript Content Webpage通过 VBA 抓取 JavaScript 内容网页
【发布时间】:2023-02-03 12:48:43
【问题描述】:

我正在尝试从Drainage Services Department 中提取一个表。我在下面写了 VBA 代码,但它不起作用。我猜是因为这个表是 JavaScript。有解决这个问题的想法吗?

Sub DSD()
    
    Dim ie As New InternetExplorer
    Dim html As New HTMLDocument
    Dim url As String
    
    url = "https://www.dsd.gov.hk/EN/Tender_Notices/Current_Tenders/index.html"
    
    ie.Visible = False
    ie.navigate url
    
    Do While ie.readyState <> READYSTATE_COMPLETE
        DoEvents
    Loop
    
    Set html = ie.document
    
    Dim lists As IHTMLElementCollection
    Dim anchorElements As IHTMLElementCollection
    Dim ulElement As HTMLUListElement
    Dim liElement As HTMLLIElement
    Dim row As Long

    Set lists = html.getElementsByClassName("ncol-md-12 result")
    row = 1
    
    For Each ulElement In lists
        For Each liElement In ulElement.getElementsByTagName("tbody")
          Set anchorElements = liElement.getElementsByTagName("td")
          If anchorElements.Length > 0 Then
              Cells(row, 1) = anchorElements.Item(0).innerText
               row = row + 1
          End If
    Next liElement
Next ulElement
    
  
    
      
    
ie.Quit
End Sub

我正试图从这个网站上抓取表格。

【问题讨论】:

    标签: javascript excel vba web-scraping


    【解决方案1】:

    尝试这个

    Sub DSD()
        
        Dim ie As New InternetExplorer
        Dim html As New HTMLDocument
        Dim url As String
        
        url = "https://www.dsd.gov.hk/EN/Tender_Notices/Current_Tenders/index.html"
        
        ie.Visible = False
        ie.navigate url
        
        Do While ie.readyState <> READYSTATE_COMPLETE
            DoEvents
        Loop
        
        Set html = ie.document
        
        Dim lists As IHTMLElementCollection
        Dim anchorElements As IHTMLElementCollection
        Dim ulElement As HTMLUListElement
        Dim liElement As HTMLLIElement
        Dim row As Long
        
        'you had put "ncol-md-12 result" it is actually named "col-md-12 result"
        Set lists = html.getElementsByClassName("col-md-12 result")
        row = 1
        
        For Each ulElement In lists
            For Each liElement In ulElement.getElementsByTagName("tbody")
              Set anchorElements = liElement.getElementsByTagName("td")
              If anchorElements.Length > 0 Then
                Debug.Print anchorElements.Item(0).innerText
                   Cells(row, 1) = anchorElements.Item(0).innerText
                   row = row + 1
              End If
            Next liElement
        Next ulElement
          
    ie.Quit
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2013-08-27
      • 2021-11-22
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      相关资源
      最近更新 更多