Pulling a google search result into excel

charlieo

New Member
Joined
Jan 4, 2014
Messages
1
Hello one and all,
I have a column of 300 keywords. I would like to run a Google search on each keyword and then populate the adjacent column with the HEADLINE TEXT from the first google result.

e.g.
keyword = cookies
Headline text from 1st search result = how to delete and control cookies

I have tried and failed to use a Web Query in Excel. I managed to get it to search a single keyword and import results, but not a list of keywords. I suspect I should be using a Macro.

I found the following macro on Stackoverflow, but don't quite know how to get it to work/run on my data. Using VBA in Excel to Google Search in IE and return the hyperlink of the first result - Stack Overflow

Any help gratefully received.
Thank you
Charlie
I can do it on my Mac with Excel 2011 or PC with Excel 2010
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello

This works for me, with keywords in column from cell A2 downwards.
Tested in IE 10, Win 7, Excel 2013.

Code:
Sub XMLHTTP()

    Dim link As Object

    On Error Resume Next

    sq = Cells(1).CurrentRegion.Resize(, 3)

    For r = 2 To UBound(sq)

        With CreateObject("MSXML2.serverXMLHTTP")
            .Open "GET", "https://www.google.co.in/search?q=" & sq(r, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000), 0
            .setRequestHeader "Content-Type", "text/xml"
            .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
            .send
            c00 = .ResponseText
        End With
        
        With CreateObject("htmlfile")
            .body.innerHTML = c00
            Set link = .getelementbyid("rso").getelementsbytagname("H3")(0).getelementsbytagname("a")(0)
        End With

        str_text = ""
        str_text = Replace(Replace(link.innerHTML, "", ""), "", "")
        sq(r, 2) = str_text
        sq(r, 3) = link.href

    Next

    Cells(1).CurrentRegion.Resize(, 3) = sq

    MsgBox "done"

End Sub
 
Upvote 0
Code:
Sub XMLHTTP()

    Dim link As Object

    On Error Resume Next

    sq = Cells(1).CurrentRegion.Resize(, 3)

    For r = 2 To UBound(sq)

        With CreateObject("MSXML2.serverXMLHTTP")
            .Open "GET", "https://www.google.co.in/search?q=" & sq(r, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000), 0
            .setRequestHeader "Content-Type", "text/xml"
            .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
            .send
            c00 = .ResponseText
        End With
        
        With CreateObject("htmlfile")
            .body.innerHTML = c00
            Set link = .getelementbyid("rso").getelementsbytagname("H3")(0).getelementsbytagname("a")(0)
        End With

        str_text = ""
        str_text = Replace(Replace(link.innerHTML, "", ""), "", "")
        sq(r, 2) = str_text
        sq(r, 3) = link.href

    Next

    Cells(1).CurrentRegion.Resize(, 3) = sq

    MsgBox "done"

End Sub

Nice code! It worked for me.
However, it seems it is only working for approximately 700 results/day.
Is there a way to increase this number?
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
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