1

I would like to extract the hyperlink from a webpage by using queryselector all, but there are no results coming out.

Below is my code.

Sub ScrapLink()

Application.ScreenUpdating = False
Dim IE As New InternetExplorer, html As HTMLDocument
Dim x As Long    
Application.ScreenUpdating = False
With IE
IE.Visible = True
IE.Navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065" 
While .Busy Or .ReadyState < 4: DoEvents: Wend
Application.Wait Now + TimeSerial(0, 0, 1)
DoEvents

With .Document.getElementById("bm_ann_detail_iframe").contentDocument
     Dim links As Object, i As Long
     Set links = .Document.querySelectorAll("p.att_download_pdf[href^='/FileAccess/apbursaweb/']")
     For i = 1 To links.Length
      With ThisWorkbook.Worksheets("Sheet1")
       Range("A" & Rows.Count).End(xlUp).Offset(1).Value = links.Item(i - 1)
    End With
   Next i
  .Quit
  End With
 End With
End Sub
1
  • change Set links = .Document.querySelectorAll("p.att_download_pdf[href^='/FileAccess/apbursaweb/']") to Set links = .querySelectorAll("a[href^='/FileAccess/apbursaweb/']") Commented Nov 19, 2018 at 12:56

2 Answers 2

2

You could just avoid the initial page and use the URL direct from the frame. This would be my preference unless you don't know, for some reason, this URL.

Option Explicit

Public Sub GetInfo()
    Dim IE As New InternetExplorer, nodeList As Object, i As Long
    With IE
        .Visible = True
        .navigate2 "http://disclosure.bursamalaysia.com/FileAccess/viewHtml?e=2906127"

        While .Busy Or .readyState < 4: DoEvents: Wend

        Set nodeList = .document.querySelectorAll(".att_download_pdf [href^='/FileAccess/apbursaweb/download']")
        For i = 0 To nodeList.Length - 1
            Debug.Print nodeList.item(i).href
        Next
        .Quit
    End With
End Sub

Or you can jump right on over to the iframe src after page load:

Option Explicit   
Public Sub GetInfo()
    Dim IE As New InternetExplorer, nodeList As Object, i As Long
    With IE
        .Visible = True
        .Navigate2 "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"

        While .Busy Or .readyState < 4: DoEvents: Wend      
        .Navigate2 .document.querySelector("iframe").src           
        While .Busy Or .readyState < 4: DoEvents: Wend

        Set nodeList = .document.querySelectorAll(".att_download_pdf [href^='/FileAccess/apbursaweb/download']")
        For i = 0 To nodeList.Length - 1
            Debug.Print nodeList.item(i).href
        Next
        .Quit
    End With
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Thank for your advice. :)
1

Try the following. It should fetch you the links you wish to grab:

Sub ScrapLink()
    Dim IE As New InternetExplorer, Html As HTMLDocument
    Dim frame As Object, i As Long

    With IE
        .Visible = True
        .navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
    End With

    Application.Wait Now + TimeValue("00:00:03") 'This delay may vary in your case

    Set frame = Html.getElementById("bm_ann_detail_iframe").contentWindow.document
    With frame.querySelectorAll("p.att_download_pdf a")
        For i = 0 To .Length - 1
            Cells(i + 1, 1) = .item(i).getAttribute("href")
        Next i
    End With
End Sub

If you wish to kick out the delay then try changing the portion below with the above one:

Do: Set frame = Html.getElementById("bm_ann_detail_iframe"): DoEvents: Loop While frame Is Nothing

With frame.contentWindow.document.querySelectorAll("p.att_download_pdf a")
    For i = 0 To .Length - 1
        Cells(i + 1, 1) = .item(i).getAttribute("href")
    Next i
End With

1 Comment

Thank for your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.