Copy URLs from Google Search Engine Results Page

I’m having difficulty writing part of a macro that copies the 10 URLs from a Google Search Engine Results page and then pastes the copied URLs into a spreadsheet. I can’t figure out how to copy/scrape just the URLs for each listing on Google’s results page. Any suggestions?

Hey Zcardais,

You don't mention what browser and what spreadsheet you're using.

This is written for Safari:

Run from an Execute an AppleScript action, and send the result to the clipboard.

Execute AppleScript { Text Script }.kmmacros (2.5 KB)

Further automation may be possible depending upon your spreadsheet software.

Running just the JavaScript from an Execute JavaScript in Safari action:

Execute a JavaScript in Safari copy.kmmacros (2.4 KB)

*Xpath JavaScript by @ComplexPoint.

-Chris

1 Like

Thanks very much.

I’m using google chrome and excel 2016 for Mac.

Thanks,

Zach C.

Hey Zach,

The AppleScript for Chrome is:

------------------------------------------------------------
set javascriptStr to "(function () {
  var xpathResults = document.evaluate('//*[@class=\\'r\\']/a', document, null, 0, null),
  nodeList = [],
  oNode;

  while (oNode = xpathResults.iterateNext()) {
    nodeList.push(oNode.href);
  }

  return nodeList.join('\\n');

})();
"

tell application "Google Chrome"
  set linkList to execute active tab of front window javascript javascriptStr
end tell

if linkList ≠ "" then
  set AppleScript's text item delimiters to linefeed
  set linkList to linkList as text
else
  error "No links were found! "
end if
------------------------------------------------------------

You can directly transplant the JavaScript from the Safari action to an Execute a JavaScript in Google Chrome action.

Excel is pretty scriptable, but the reports on Office 2016 are that scripting is still a bit flakey.

I have no intention of upgrading unless/until this is resolved.

NOTE: All Google Chrome actions are run via AppleScript under-the-hood, and Chrome’s AppleScript support can be flakey at times. If you experience problems the first thing to try when troubleshooting is to quit and restart Chrome.

-Chris

Chris, many thanks for sharing this macro/script.
Based on your script, I've created this full featured macro that works with both Safari and Chrome, and provides options for output.