scrape data from web page source where url doesn't change

前端 未结 1 507
日久生厌
日久生厌 2021-01-24 21:34

I need to do the following

  • Go to http://healthapps.state.nj.us/facilities/acSetSearch.aspx?by=county

  • Select \"Special Hospital\

相关标签:
1条回答
  • 2021-01-24 21:51

    It navigates to each result page, and back to homepage in between so as to leverage the postBack links through clicks.

    Option Explicit
    Public Sub VisitPages()
        Dim IE As New InternetExplorer
        With IE
            .Visible = True
            .navigate "http://healthapps.state.nj.us/facilities/acSetSearch.aspx?by=county"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            With .document
                .querySelector("#middleContent_cbType_5").Click
                .querySelector("#middleContent_cbType_12").Click
                .querySelector("#middleContent_btnGetList").Click
            End With
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            Dim list As Object, i  As Long
            Set list = .document.querySelectorAll("#main_table [href*=doPostBack]")
            For i = 0 To list.Length - 1
                list.item(i).Click
    
                While .Busy Or .readyState < 4: DoEvents: Wend
    
                Application.Wait Now + TimeSerial(0, 0, 3) '<== Delete me later. This is just to demo page changes
                'do stuff with new page
                .Navigate2 .document.URL             '<== back to homepage
                While .Busy Or .readyState < 4: DoEvents: Wend
                Set list = .document.querySelectorAll("#main_table [href*=doPostBack]") 'reset list (often required in these scenarios)
            Next
            Stop                                     '<== Delete me later
            '.Quit '<== Remember to quit application
        End With
    End Sub
    

    Same thing with executing the postBacks

    Option Explicit
    Public Sub VisitPages()
        Dim IE As New InternetExplorer
        With IE
            .Visible = True
            .navigate "http://healthapps.state.nj.us/facilities/acSetSearch.aspx?by=county"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            With .document
                .querySelector("#middleContent_cbType_5").Click
                .querySelector("#middleContent_cbType_12").Click
                .querySelector("#middleContent_btnGetList").Click
            End With
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            Dim list As Object, i  As Long, col As Collection
            Set col = New Collection
            Set list = .document.querySelectorAll("#main_table [href*=doPostBack]")
            For i = 0 To list.Length - 1
               col.Add CStr(list.item(i))
            Next
            For i = 1 To col.Count
                .document.parentWindow.execScript col.item(i)
                 While .Busy Or .readyState < 4: DoEvents: Wend
                'Do stuff with page
                .Navigate2 .document.URL
                While .Busy Or .readyState < 4: DoEvents: Wend
            Next
            Stop                                     '<== Delete me later
            '.Quit '<== Remember to quit application
        End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题