get data from site with dropdown filter

born2be

New Member
Joined
May 29, 2016
Messages
20
in need to get data from a site that search a doctor name in spec. city

i have some of the code and thats working but on the site you can filter is specialism thats now the part that i don't can add in my code
so i can pull data filtering by city and name so that specialism a want to add in my filter before i pull the data from the site
i insert a pic with the option marked that i want to add in my code

thx



VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = Range("naam").Row And Target.Column = Range("naam").Column Then
If Target.Row = Range("gemeente").Value > "" Then 'Row And Target.Column = Range("gemeente").Column Then
Dim ie As New InternetExplorer
ie.Visible = True
ie.navigate "https://www.ordomedic.be/nl/zoek-een-arts/?search_name=" & Range("naam") & "&+search_place=" & Range("gemeente") & "&+search_specialism"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = ie.document
Dim  sdd2, sdd3, sdd4, sdd5, sdd0 As String
sdd2 = Trim(doc.getElementsByTagName("dd")(2).innerText)
sdd3 = Trim(doc.getElementsByTagName("dd")(3).innerText)
sdd4 = Trim(doc.getElementsByTagName("dd")(4).innerText)
sdd5 = Trim(doc.getElementsByTagName("dd")(5).innerText)
sdd0 = Trim(doc.getElementsByTagName("dd")(0).innerText)
Dim LString As String
Dim LArray() As String

LString = sdd0
LArray = Split(LString, " ")

Range("k10") = LArray(1) & " " & LArray(0)
'Range("k10") = LArray(1)
'Range("k10") = sdd0
Range("k11") = sdd2
Range("k12") = sdd3
Range("k13") = sdd5

End If
End If
End Sub
 

Attachments

  • pull data from.png
    pull data from.png
    74.4 KB · Views: 18

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Do you still need help with this?

You have cells containing the Name and Place; how would the Specialism be specified by the user? Can they specify more than one Specialism? If so, how?
 
Upvote 0
yes i have cells containing name place and street and phone
VBA Code:
Range("k10") = LArray(1) & " " & LArray(0) '= name and lastname

Range("k11") = sdd2  '=street and number
Range("k12") = sdd3 '=postalcode and city
Range("k13") = sdd5 '=phone number

i hope you con add in my search url where i search but specialsim dont work in this "https://www.ordomedic.be/nl/zoek-een-arts/?search_name=" & Range("naam") & "&+search_place=" & Range("gemeente") & "&+search_specialism="&Range("Algemene_geneeskunde")
 
Upvote 0
You didn't answer my two questions, so I assume you want to search for only 1 Specialism, "Algemene geneeskunde", and this text is typed in a cell.

Your OP shows 2 named ranges for the Name ("naam") and Place ("gemeente") - B1 and B2 in the sheet below. The code requires 2 more named ranges which you must create:

"specialisme" - the cell containing the Specialism text - B3 in the sheet below.

"resultsName" - the cell containing the Name heading - A6 in the sheet below. The data is extracted and put into the first empty row below the "resultsName" cell heading.

Rather than doing the search from the Worksheet_Change event handler, the IE_Main_Search macro below should be called from a form button placed on the sheet. The user can then type in the 3 cells and click the button to run the search.

Note that the code can search for multiple Specialisms - simply separate them with commas in the "specialisme" cell. The code extracts results from multiple pages, up to a maximum of 20 pages (200 results) when the web page displays: "Het aantal resultaten dat kan worden bekeken is beperkt tot 200. Gelieve u aan te melden om toegang te krijgen tot de volgende zoekresultaten".

IE Search.xlsm
ABCDEFG
1Name
2Postcode or townDadizele
3Specialism(s)Algemene geneeskunde
4
5
6NameSpecialismeAddress1Address2Address3Address4Telephone
7COOREMAN EvaAlgemene geneeskundep.a. Azalealaan 83A8890 DadizeleBelgium056/50.58.92
8CORYN JanAlgemene geneeskundeBeselarestraat 428890 DadizeleBelgium056/50.60.79
9DE SMET EvaAlgemene geneeskundep.a. Azalealaan 83A8890 DadizeleBelgium056/50.58.92
10DUMONT RaymondAlgemene geneeskundeSperredreef 208890 DadizeleBelgium056/50.97.99
11VANDERSTRAETEN LouisAlgemene geneeskundep.a. Azalealaan 838890 DadizeleBelgium056/50.58.92
12VANKEIRSBILCK MariekeAlgemene geneeskundeAzalealaan 83A8890 DadizeleBelgium056/50.58.92
Search

Paste this code into a standard module. It requires the 2 references shown at the top of the code, which you must select via Tools -> References in the VBA editor, otherwise the code won't compile or run.

VBA Code:
'Required references:
'Microsoft HTML Object Library
'Microsoft Internet Controls

Option Explicit


#If VBA7 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As LongPtr) As LongPtr
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
    Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If
  
    
Public Sub IE_Main_Search()

    Dim resultsBaseCell As Range
  
    With Range("resultsName").CurrentRegion
        Set resultsBaseCell = .Offset(.Rows.Count).Item(1)
    End With

    IE_POST_Form_Search Range("naam").Value, Range("gemeente").Value, Range("specialisme").Value, resultsBaseCell

End Sub


Private Sub IE_POST_Form_Search(searchName As String, searchPlace As String, searchSpecialisms As String, resultsBaseCell As Range)

    Dim IE As InternetExplorer
    Dim URL As String
    Dim HTMLdoc As HTMLDocument
    Dim searchForm As HTMLFormElement
    Dim specialismSelect As HTMLSelectElement
    Dim results As IHTMLElementCollection
    Dim resultDiv As HTMLDivElement
    Dim DDelements As IHTMLElementCollection
    Dim nextPage As HTMLAnchorElement
    Dim specialism As Variant
    Dim optionValue As String
    Dim formData As String
    Dim i As Long, n As Long, page As Long
        
    URL = "https://www.ordomedic.be/nl/zoek-een-arts/"
    
    Set IE = Get_IE_Window2(URL)
    If IE Is Nothing Then Set IE = New InternetExplorer
    
    With IE
        SetForegroundWindow .hwnd
        .navigate URL
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        .Visible = True
        Set HTMLdoc = .document
    End With
    
    'Get the search form which contains the action URL for sending the form data to
    '<form action="https://www.ordomedic.be/nl/zoek-een-arts/" method="post" id="search-register" class="fullpage">

    Set searchForm = HTMLdoc.getElementById("search-register")
    
    'Get the Specialism select element.  The option value(s) are sent in the form data according to the specified specialism(s)
    '<select style="width: 207px; display: none;" multiple="multiple" name="search_specialism[]" id="search_specialism">
    '        <option value="24" label="Acute geneeskunde">Acute geneeskunde</option>
    '        <option value="32" label="Algemene geneeskunde">Algemene geneeskunde</option>
    '        <option value="2" label="Anesthesie-reanimatie">Anesthesie-reanimatie</option>
    '        <option value="37" label="Arbeidsgeneeskunde">Arbeidsgeneeskunde</option>
    '        <option value="40" label="Arts" selected="selected">Arts</option>
    '        <option value="19" label="Cardiologie" selected="selected">Cardiologie</option>
    ' :
    '        <option value="11" label="Urologie">Urologie</option>
    '        <option value="35" label="Verzekeringsgeneeskunde en medische expertise">Verzekeringsgeneeskunde en medische expertise</option>
    '</select>

    Set specialismSelect = HTMLdoc.getElementById("search_specialism")
    
    'Construct form data string with the following field names (example field values):
    'search_name:
    'search_place: Dadizele
    'search_place_min:
    'search_place_max:
    'search_specialism[]: 32
    'search_specialism[]: 40
    
    formData = URLEncode("search_name") & "=" & URLEncode(searchName) & _
               "&" & URLEncode("search_place") & "=" & URLEncode(searchPlace) & _
               "&" & URLEncode("search_place_min") & "=" & _
               "&" & URLEncode("search_place_max") & "="

    'Add specialism option values(s) to form data

    For Each specialism In Split(searchSpecialisms, ",")
        optionValue = FindSelectOptionValue(specialismSelect, CStr(specialism))
        If optionValue <> "" Then
            formData = formData & "&" & URLEncode("search_specialism[]") & "=" & URLEncode(optionValue)
        End If
    Next
    
    'Send form data in IE POST request to perform search and get first page of results
    
    IE_Post_Form_Data IE, searchForm.Action, formData

    n = 0
    page = 1
    
    Do
    
        'Extract data from this results page
        
        Set results = HTMLdoc.getElementsByClassName("result")
        
        If results.Length > 0 Then
        
            For Each resultDiv In results
                
                'Name and specialism
                
                Set DDelements = resultDiv.Children(0).getElementsByTagName("DD")
                For i = 0 To DDelements.Length - 1
                    resultsBaseCell.Offset(n, i).Value = DDelements(i).innerText
                Next
                
                'Address1, Address2, Address3, Address4 and Telephone
                
                Set DDelements = resultDiv.Children(1).getElementsByTagName("DD")
                For i = 0 To DDelements.Length - 1
                    If InStr(DDelements(i).innerText, "Tel.: ") = 1 Then
                        resultsBaseCell.Offset(n, 6).Value = Mid(DDelements(i).innerText, 7)
                    Else
                        resultsBaseCell.Offset(n, i + 2).Value = DDelements(i).innerText
                    End If
                Next
                
                n = n + 1
                
            Next
        
        Else
        
            resultsBaseCell.Offset(n, 0).Value = "No results found"
            n = n + 1
            
        End If
        
        'Find link for page number 'page' of results and click it
        
        page = page + 1
        Set nextPage = Nothing
        i = 0
        While i < HTMLdoc.Links.Length And nextPage Is Nothing
            If InStr(HTMLdoc.Links(i).href, "&page=" & page) > 0 Then Set nextPage = HTMLdoc.Links(i)
            i = i + 1
        Wend
    
        If Not nextPage Is Nothing Then
            With IE
                .navigate nextPage.href
                While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
            End With
        End If
    
        'Loop until no more pages or maximum of 200 results has been reached
        
    Loop Until nextPage Is Nothing Or _
               InStr(1, HTMLdoc.body.innerText, "Het aantal resultaten dat kan worden bekeken is beperkt tot 200. Gelieve u aan te melden om toegang te krijgen tot de volgende zoekresultaten")
    
End Sub


Private Sub IE_Post_Form_Data(IE As InternetExplorer, URL As String, formData As String)
  
    'Send URL-encoded form data to the URL using IE
  
    Dim bFormData() As Byte
    ReDim bFormData(Len(formData) - 1)
  
    bFormData = StrConv(formData, vbFromUnicode)
    With IE
        .navigate URL, PostData:=bFormData, Headers:="Content-type: application/x-www-form-urlencoded" & vbCrLf
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Sleep 100: Wend
    End With
  
End Sub


Private Function URLEncode(data As String) As String
  
    'URL-encode of a string data
  
    Dim i As Long, c As Integer
  
    URLEncode = ""
    For i = 1 To Len(data)
        c = Asc(Mid(data, i, 1))
        Select Case c
            Case Is = 32: URLEncode = URLEncode & "+"
            Case Is < 48: URLEncode = URLEncode & "%" & Hex(c)
            Case Else:    URLEncode = URLEncode & Mid(data, i, 1)
        End Select
    Next

End Function


Private Function FindSelectOptionValue(selectElement As HTMLSelectElement, findOptionText As String) As String

    'Return the option value for the specified option text

    Dim i As Long
  
    FindSelectOptionValue = ""
    i = 0
    While i < selectElement.Options.Length And FindSelectOptionValue = ""
        If selectElement.Item(i).Text = findOptionText Then FindSelectOptionValue = selectElement.Item(i).Value
        i = i + 1
    Wend

End Function


Private Function Get_IE_Window2(URLorName As String) As InternetExplorer

    'Look for an IE browser window or tab already open at the (partial) URL or location name and, if found, return
    'that browser as an InternetExplorer object.  Otherwise return Nothing

    Dim Shell As Object
    Dim IE As InternetExplorer
    Dim i As Variant 'Must be a Variant to index Shell.Windows.Item() array
  
    Set Shell = CreateObject("Shell.Application")
  
    i = 0
    Set Get_IE_Window2 = Nothing
    While i < Shell.Windows.Count And Get_IE_Window2 Is Nothing
        Set IE = Shell.Windows.Item(i)
        If Not IE Is Nothing Then
            If IE.Name = "Internet Explorer" And InStr(IE.LocationURL, "file://") <> 1 Then
                If InStr(1, IE.LocationURL, URLorName, vbTextCompare) > 0 Or InStr(1, IE.LocationName, URLorName, vbTextCompare) > 0 Then
                    Set Get_IE_Window2 = IE
                End If
            End If
        End If
        i = i + 1
    Wend
  
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top