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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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