Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 31

Thread: Lookup First URL From Google Search Result Using VBA

  1. #21
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Try this

    Code:
    Option Explicit
    
    Sub lm_GetGooleFirstSearchAddres()
    
    
        Dim strHtml                 As String
        Dim lngLoop                 As Long
        Dim rngRange                As Range
        Dim rngCell                 As Range
        
        With ThisWorkbook.Worksheets("Sheet1")
            Set rngCell = .Range("A1").CurrentRegion.Resize(, 1)
            Set rngRange = Nothing
            On Error Resume Next
            Set rngRange = Intersect(rngCell, rngCell.Offset(1))
            On Error GoTo -1: On Error GoTo 0: Err.Clear
            If Not rngRange Is Nothing Then
                Set objMXHTML = CreateObject("MSXML2.XMLHTTP")
                For Each rngCell In rngRange
                    With objMXHTML
                        .Open "get", URLEncode(rngCell.Value), False
                        .send
                        strHtml = .responseText
                        strHtml = Mid(strHtml, InStr(1, strHtml, "<div id=""ires"">"))
                        strHtml = Mid(strHtml, 1, InStr(1, strHtml, "<div class=""s"">"))
                        strHtml = Mid(strHtml, InStr(1, strHtml, "<a href="))
                        strHtml = Mid(strHtml, InStr(1, strHtml, "<a href="), InStr(1, strHtml, """ onmousedown="""))
                        strHtml = Mid(strHtml, InStr(1, strHtml, """"), Len(strHtml) - 1)
                        rngCell.Offset(, 1).Value = Mid(Trim(strHtml), 2, Len(Trim(strHtml)) - 2)
                    End With
                Next rngCell
                MsgBox "Search completed.", vbInformation, "Google Search..."
            Else
                MsgBox "No data found to search.", vbCritical, "Google Search..."
            End If
        End With
        
        strHtml = vbNullString
        lngLoop = Empty
        Set rngRange = Nothing
        Set rngCell = Nothing
        
    End Sub
    
    
    Function URLEncode(EncodeStr As String) As String
        
        Dim i As Integer
        Dim erg As String
        
        Const strGogSrchUrl         As String = " http://www.google.co.in/search?output=search&sclient=psy-ab&q=|||&btnK="
        Const strConcatDelima       As String = "|||"
        
        erg = EncodeStr
    
    
        ' *** First replace '%' chr
        erg = Replace(erg, "%", Chr(1))
    
    
        ' *** then '+' chr
        erg = Replace(erg, "+", Chr(2))
        
        For i = 0 To 255
            Select Case i
                ' *** Allowed 'regular' characters
                Case 37, 43, 48 To 57, 65 To 90, 97 To 122
                
                Case 1  ' *** Replace original %
                    erg = Replace(erg, Chr(i), "%25")
            
                Case 2  ' *** Replace original +
                    erg = Replace(erg, Chr(i), "%2B")
                    
                Case 32
                    erg = Replace(erg, Chr(i), "+")
            
                Case 3 To 15
                    erg = Replace(erg, Chr(i), "%0" & Hex(i))
            
                Case Else
                    erg = Replace(erg, Chr(i), "%" & Hex(i))
                    
            End Select
        Next
        
        URLEncode = Replace(strGogSrchUrl, strConcatDelima, erg)
        'URLEncode = strGogSrchUrl & erg
        
        i = Empty
        erg = vbNullString
        
    End Function
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #22
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0
    when I run this code I get an error. compile error: Variable not found.

  3. #23
    Moderator
    Join Date
    Jul 2012
    Posts
    156
    Rep Power
    13
    Code:
    Dim objMXHTML As Object

  4. #24
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0
    still get the error 419 permission to use the object denied ?

  5. #25
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0
    Hi Lalit i have been using your code, it works fine after few minutes i get this crazy error "permission to use the object denied" error 419. i tried few things altering the code, didn't work, so the best person to ask is the creator, any help?

  6. #26
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Rep Power
    0

    lOOKUP fIRST url fIX 3/2018

    Excel Fox,
    The macro you created no longer works. I think the html page formatting has changed since you wrote it.
    I am definitely no expert but I rewrote It anyway. It's messy but seems much faster and did not crap out during 4200 entry search.
    Regards
    Spike

    File attached:
    Attached Files Attached Files

  7. #27
    Junior Member
    Join Date
    Mar 2018
    Posts
    2
    Rep Power
    0
    Sorry about caps lock . . .
    Here is Code from file:

    Code:
    Option Explicit
    Dim var As Variant
    
    Sub SearchBing()
        
        Dim rng As Range
        Dim lngCol As Long
        Dim oXML As New MSXML2.XMLHTTP
        Dim uTxt As Variant
        Dim xURL As String
        
        Dim srchString As Variant
        Dim StrStart As Variant
        Dim StrEnd As Variant
        Dim StrOutput As Variant
        Dim x As Variant
        Dim OutputRowNumber As Variant
        
        ' Clear Destination Column for URL Lookup
        Worksheets("Sheet1").Range("B2:B" & Rows.Count).ClearContents
        
          
        ' Set base search using bing instead of google because google's pages are a mess
        Const strURL = "http://www.bing.com/search?q=|+|"
        ' Move through source cells
        For Each rng In Worksheets("Sheet1").Range("A2:A5000")
            ' Make sure range is not empty before we start
            If Not IsEmpty(rng) Then
                ' encode url for direct submission
                xURL = Replace(strURL, "|+|", URLEncode(rng.Value))
                'Debug.Print xURL
                ' set object parameters
                oXML.Open "GET", xURL, False
                oXML.setRequestHeader "Content-Type", "text/xml"
                
                ' Delay request by wait to allow catchup and to not freakout
                ' search firewalls
                x = x + 1
                If x = 6 Then
                x = 0
                'Slow it down a bit
                DoEvents
                End If
                'Debug.Print "x=" & x
                           
                ' if you don't have this program will crap out if it encounters weird
                ' formatted page
                On Error Resume Next
                
                'Send request returns page of searchable xml text to oXML object
                oXML.send
                'FIRST SEARCH PRIORITY
                ' srchstring finds unique string in the ballpark of what you want
                ' add more searchs if you want other information or if you want to
                ' get check more accurate info from page if it is available
                ' this section finds "website" button that sometimes appears on the right
                ' of bing search results
                srchString = "role=""button"" href=""http"
                ' extract small chunk of text so you can search again without dupes
                StrStart = InStr(1, oXML.responseText, srchString) + 20
                StrEnd = InStr(StrStart, oXML.responseText, 60)
                StrOutput = Mid(oXML.responseText, StrStart, StrEnd - StrStart - 3) & Chr(10)
                
                ' in the smaller string you extracted above drill down on the actual string
                ' you want - this one finds the first instance of http in the smaller string
                StrStart = InStr(1, StrOutput, "http")
                ' this one finds the end of the url by starting 9 after http and looks for "
                StrEnd = InStr(StrStart + 9, StrOutput, """")
                ' use start and end to isolate the web address
                StrOutput = Mid(StrOutput, StrStart, StrEnd - StrStart)
                
                
                
                'SECOND SEARCH PRIORITY
                ' using if-then If the first search does not find a website button it
                ' returns first item from search results. Note: First result is not always
                ' what you want
                If InStr(1, StrOutput, "www.w3.org") Then
                        srchString = "Search Results"
                        StrStart = InStr(1, oXML.responseText, srchString) + 47
                        StrEnd = InStr(StrStart, oXML.responseText, 60)
                        StrOutput = Mid(oXML.responseText, StrStart, StrEnd - StrStart - 3) & Chr(10)
                
                 
                        StrStart = InStr(1, StrOutput, "http")
                        StrEnd = InStr(StrStart + 9, StrOutput, """")
                        StrOutput = Mid(StrOutput, StrStart, StrEnd - StrStart)
                         
                End If
                
                'Remove results that is too long (likely some sort of error)
                If Len(StrOutput) > 50 Then StrOutput = "NOT FOUND"
                
                'Cleanup Some of the Bad Addresses
                If InStr(1, StrOutput, "bloomberg.com") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "dandb.com") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "bing.com") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "wikipedia.org") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "doubleclick.net") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "brightscope.com") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "usnews.com") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "linkedin.com") Then StrOutput = "NOT FOUND"
                If InStr(1, StrOutput, "usnews.com") Then StrOutput = "NOT FOUND"
                
                
                
                'add row numbers for debugging
                'OutputRowNumber = OutputRowNumber + 1
                'Debug.Print OutputRowNumber & ": " & StrOutput
                'Debug.Print oXML.responseText
                
                rng.Offset(, 1).Value = StrOutput
                
            Else
                Exit For
            End If
        Next rng
        'set debugging row back to zero
        'OutputRowNumber = 0
        Set rng = Nothing
        
    
    End Sub
    
    
    Function URLEncode(EncodeStr As String) As String
        Dim i As Integer
        Dim erg As String
        
        erg = EncodeStr
    
        ' *** First replace '%' chr
        erg = Replace(erg, "%", Chr(1))
    
        ' *** then '+' chr
        erg = Replace(erg, "+", Chr(2))
        
        For i = 0 To 255
            Select Case i
                ' *** Allowed 'regular' characters
                Case 37, 43, 48 To 57, 65 To 90, 97 To 122
                
                Case 1  ' *** Replace original %
                    erg = Replace(erg, Chr(i), "%25")
            
                Case 2  ' *** Replace original +
                    erg = Replace(erg, Chr(i), "%2B")
                    
                Case 32
                    erg = Replace(erg, Chr(i), "+")
            
                Case 3 To 15
                    erg = Replace(erg, Chr(i), "%0" & Hex(i))
            
                Case Else
                    erg = Replace(erg, Chr(i), "%" & Hex(i))
                    
            End Select
        Next
        
        URLEncode = erg
        
    End Function

  8. #28
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Thank you Spike for sharing the modified version. Much appreciated.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  9. #29
    Junior Member
    Join Date
    Aug 2018
    Posts
    2
    Rep Power
    0
    please my friend , i want VBa like this Vba above to extract email for domain but i want to extract emails of per domain from google and bing and yahoo to extract all emails of per domain in another page excel

  10. #30
    Junior Member
    Join Date
    Jan 2019
    Posts
    1
    Rep Power
    0
    Hello Excel Fox; Spike;

    Thanks for the thread and the updates.

    As regards to the last file submitted by Spike, the results extracted are only "http://www.w3.org/1999/xhtml".
    I'm not sure I understand why there is a second search priority like yours .. Could you give me more info ?

    Moreover, would this code apply to a google search ?

    Thanks for your return

Similar Threads

  1. Replies: 4
    Last Post: 04-24-2013, 10:04 AM
  2. Find the highest then lookup result
    By Stalker in forum Excel Help
    Replies: 4
    Last Post: 04-02-2013, 02:04 PM
  3. Import html source of url list in each cell
    By Sergio Alfaro Lloret in forum Excel Help
    Replies: 8
    Last Post: 07-31-2012, 03:03 AM
  4. VBA Function to Search in Array
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 04-10-2012, 11:34 AM
  5. VBA - Excel: Disable Internet / Google
    By technicalupload in forum Excel Help
    Replies: 3
    Last Post: 10-06-2011, 09:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •