jeudi 2 janvier 2020

Search a website using excel vba with excel data and extract the active state in flowchart of search result and mapping it into column

I am hoping someone can help....

I have around 7000 values in a excel spreadsheet that I need to search in a website and then record active state of result flowchart from the website to be inputted back into the excel spreadsheet. Since I am new to macros web scrap I used to automate web code modified input ids for the website which I want to extract information (https://nacionalidade.justica.gov.pt/). I am a bit confused in how to apply if condition to get the active state having seven classes in flowhchart, Here is the flow chart. enter image description here

Now that I have access codes each will be on different stage, I only want to pick the state and put it in column E in front of the access code(currently doing manually)enter image description here

I am unclear how to extract that info being new to this type of web data extraction - any help would be incredible!

Here are the sample access codes at different levels. Level 1-**

0409-6187-1986

**

Level 2,3,4,5- **

8907-1862-1824

** (behaviour of all are same in inpect element div classes-active1 appears after the latest stage in green color)

Level 6- **

6505-9934-1884

** (div class step6 active3 appears with orange color when inpect element)

Level 7- **

4201-5973-1614

** (div class step 7 active3 appears with orange color but step6 goes to white color) enter image description here

Here is my code:(couldn't be able to change for mentioned web after this)

objIE.document.getElementById("btnPesquisa").Click

Code:

'start a new subroutine called SearchBot
Sub SearchBot()

    'dimension (declare or set aside memory for) our variables
    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
    Dim y As Integer 'integer variable we'll use as a counter
    Dim result As String 'string variable that will hold our result link

    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorer

    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True

    'navigate IE to this web page (a pretty neat search engine really)
    objIE.navigate "https://nacionalidade.justica.gov.pt/"

    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'in the search box put cell "A2" value, the word "in" and cell "C1" value
    objIE.document.getElementById("SenhaAcesso").Value = _
      Sheets("Guy Touti").Range("D2").Value

    'click the 'go' button
    objIE.document.getElementById("btnPesquisa").Click

    'wait again for the browser
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'the first search result will go in row 2
    y = 2

    'for each <a> element in the collection of objects with class of 'result__a'...
    For Each aEle In objIE.document.getElementsByClassName("result__a")

        '...get the href link and print it to the sheet in col C, row y
        result = aEle
        Sheets("Guy Touti").Range("E" & y).Value = result

        '...get the text within the element and print it to the sheet in col D
        Sheets("Guy Touti").Range("D" & y).Value = aEle.innerText
        Debug.Print aEle.innerText

        'is it a yellowpages link?
        If InStr(result, "yellowpages.com") > 0 Or InStr(result, "yp.com") > 0 Then
            'make the result red
            Sheets("Guy Touti").Range("C" & y).Interior.ColorIndex = 3
            'place a 1 to the left
            Sheets("Guy Touti").Range("B" & y).Value = 1
        End If

        'increment our row counter, so the next result goes below
        y = y + 1

    'repeat times the # of ele's we have in the collection
    Next

    'add up the yellowpages listings
    Sheets("Guy Touti").Range("B1").Value = _
      Application.WorksheetFunction.Sum(Sheets("Guy Touti").Range("B2:B100"))

    'close the browser
    objIE.Quit

'exit our SearchBot subroutine
End Sub

I did try this first but after a while started searching for a better way. Can you help????




Aucun commentaire:

Enregistrer un commentaire