How to grab web data into Numbers sheet from Google finance

Hello, here I go again, now I am trying to grab the value out of a webpage https://www.google.com/finance/quote/ACS:BME to paste it in the Numbers sheet that I am using.

The data is the is the actual price value in the site, located underneath "Grupo ACS".


The script that I am using now for other quote, grabs the data, opens the numbers file and paste the data:

--------------------------------------

set mySheetName to "2020"

set myTableName to "Tabla 1"

set tgtCell to "J26"


set theHtml to do shell script "curl -s " & quoted form of "https://www.bolsasymercados.es/MTF_Equity/esp/SICAV/Ficha/ACATES_SISAPON__SICAV__S_A__ES0176200030.aspx"

set text item delimiters to {"<div class=\"row fechaPreciosSup\">", "<div class=\"row fechaPrecio\">"}

set the extractedDivs to theHtml's text item 2 -- item 2 is the relevant section of the html

set text item delimiters to {"<p>", "</p>"} -- remove the p tags

set tempLst to text items of extractedDivs

set text item delimiters to ""

set tempStr to tempLst as text

set text item delimiters to {"<div class=\"col-md-6 col-sm-6 col-xs-6\">"}

set theValue to first word of text item 3 of tempStr

set the clipboard to theValue

-- return theValue -- uncomment this line for testing -- will appear in Result pane

tell application "Numbers"

open "/Users/ricardo/Library/Mobile Documents/com~apple~Numbers/Documents/ES Acciones.numbers"

end tell

tell application "Numbers"

tell front document

tell active sheet to set currSheet to its name # save current location

tell sheet mySheetName to tell table myTableName

activate

set selection range to range tgtCell

delay 0.3

tell application "System Events" to keystroke "v" using {option down, shift down, command down}

end tell

activate

set active sheet to sheet currSheet # go back to original location

end tell

end tell

--------------------------------


I tried other sites, but google finances seams a simpler site to make it work, but I a have not been able to make it work.


Any ideas?


Thanks in advance

Posted on Jun 30, 2021 4:33 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 12, 2021 11:03 AM

It would help if you explained what's not working.


While there are some flaws in your approach, the concept is valid, and it seems to 'work' for me. Therefore what's missing is an understanding of what isn't working.


AppleScript is usually pretty good about giving you an error message if something goes wrong. Knowing what that error message is might help


Off hand, the obvious places it could fail are:


	open "/Users/ricardo/Library/Mobile Documents/com~apple~Numbers/Documents/ES Acciones.numbers"


The above line will fail if the file in question can't be found.


		tell sheet mySheetName to tell table myTableName


This will fail if there's no sheet with the given name, or there's no table with the given name


			set selection range to range tgtCell


This will fail If the given table doesn't extent that far (e.g. you can't select a cell in column J if the table only goes up to F)



On a bigger issue, I recommend staying away from using the clipboard for this - think of AppleScript variables as unlimited, named clipboards you can use, without interfering with the user's pre-existing clipboard (how do you know the user didn't already have something important on their clipboard, which your script has now overridden?)


I also recommend avoiding anything that changes the look of Numbers.app. There is absolutely no reason to activate the app, change focus to a different window/sheet. This is also prone to error, especially when you introduce artificial delays - what happens, for example, if the user clicks on a different window between when your script selects the cell and 'pastes' the value in?


Your entire script can be simplified, as well as made more robust and faster by nixing the 'set the clipboard...' line, and replacing teh entire tell application Numbers block with something like this:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set mySheetName to "2020"
set myTableName to "Table 1"
set tgtCell to "B5"

set theHtml to do shell script "curl -s " & quoted form of "https://www.bolsasymercados.es/MTF_Equity/esp/SICAV/Ficha/ACATES_SISAPON__SICAV__S_A__ES0176200030.aspx"
set text item delimiters to {"<div class=\"row fechaPreciosSup\">", "<div class=\"row fechaPrecio\">"}
set the extractedDivs to theHtml's text item 2 -- item 2 is the relevant section of the html
set text item delimiters to {"<p>", "</p>"} -- remove the p tags
set tempLst to text items of extractedDivs
set text item delimiters to ""
set tempStr to tempLst as text
set text item delimiters to {"<div class=\"col-md-6 col-sm-6 col-xs-6\">"}
set theValue to first word of text item 3 of tempStr
-- NO! Don't set the clipboard EVER
-- set the clipboard to theValue
-- return theValue -- uncomment this line for testing -- will appear in Result pane
tell application "Numbers"
	set myDoc to open "/Users/ricardo/Library/Mobile Documents/com~apple~Numbers/Documents/ES Acciones.numbers"
end tell
tell application "Numbers"
	tell table myTableName of sheet mySheetName of myDoc
		set value of cell tgtCell to theValue
	end tell
end tell



This should work whether or not the document is already open, whether the application is frontmost or not. It won't change window focus, or interrupt the user's workflow (you could literally be doing almost anything else while this script is running and it should still work.

Similar questions

8 replies
Question marked as Top-ranking reply

Jul 12, 2021 11:03 AM in response to SonyMiner

It would help if you explained what's not working.


While there are some flaws in your approach, the concept is valid, and it seems to 'work' for me. Therefore what's missing is an understanding of what isn't working.


AppleScript is usually pretty good about giving you an error message if something goes wrong. Knowing what that error message is might help


Off hand, the obvious places it could fail are:


	open "/Users/ricardo/Library/Mobile Documents/com~apple~Numbers/Documents/ES Acciones.numbers"


The above line will fail if the file in question can't be found.


		tell sheet mySheetName to tell table myTableName


This will fail if there's no sheet with the given name, or there's no table with the given name


			set selection range to range tgtCell


This will fail If the given table doesn't extent that far (e.g. you can't select a cell in column J if the table only goes up to F)



On a bigger issue, I recommend staying away from using the clipboard for this - think of AppleScript variables as unlimited, named clipboards you can use, without interfering with the user's pre-existing clipboard (how do you know the user didn't already have something important on their clipboard, which your script has now overridden?)


I also recommend avoiding anything that changes the look of Numbers.app. There is absolutely no reason to activate the app, change focus to a different window/sheet. This is also prone to error, especially when you introduce artificial delays - what happens, for example, if the user clicks on a different window between when your script selects the cell and 'pastes' the value in?


Your entire script can be simplified, as well as made more robust and faster by nixing the 'set the clipboard...' line, and replacing teh entire tell application Numbers block with something like this:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set mySheetName to "2020"
set myTableName to "Table 1"
set tgtCell to "B5"

set theHtml to do shell script "curl -s " & quoted form of "https://www.bolsasymercados.es/MTF_Equity/esp/SICAV/Ficha/ACATES_SISAPON__SICAV__S_A__ES0176200030.aspx"
set text item delimiters to {"<div class=\"row fechaPreciosSup\">", "<div class=\"row fechaPrecio\">"}
set the extractedDivs to theHtml's text item 2 -- item 2 is the relevant section of the html
set text item delimiters to {"<p>", "</p>"} -- remove the p tags
set tempLst to text items of extractedDivs
set text item delimiters to ""
set tempStr to tempLst as text
set text item delimiters to {"<div class=\"col-md-6 col-sm-6 col-xs-6\">"}
set theValue to first word of text item 3 of tempStr
-- NO! Don't set the clipboard EVER
-- set the clipboard to theValue
-- return theValue -- uncomment this line for testing -- will appear in Result pane
tell application "Numbers"
	set myDoc to open "/Users/ricardo/Library/Mobile Documents/com~apple~Numbers/Documents/ES Acciones.numbers"
end tell
tell application "Numbers"
	tell table myTableName of sheet mySheetName of myDoc
		set value of cell tgtCell to theValue
	end tell
end tell



This should work whether or not the document is already open, whether the application is frontmost or not. It won't change window focus, or interrupt the user's workflow (you could literally be doing almost anything else while this script is running and it should still work.

Jul 14, 2021 8:42 AM in response to Camelot

Hello Camelot,


Thanks for the explanation. The example that I posted does work, and yours even better and more efficiently!! what I meant was to grab the quote form Google's " https://www.google.com/finance/quote/ACS:BME ", instead of the example https://www.bolsasymercados.es...



I re-converted what you proposed to make it work for the value under the name ACS in the link https://www.google.com/finance/quote/ACS:BME  and made it work, but the problem I have now is that the value that I get is in US system instead of the metric smite. I get a 22.xx and I need a 22,xx


How do I convert the 22.xx to 22,xx (exchange the “.” For “,”)


This is what I have:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set mySheetName to "2020"
set myTableName to "Tabla 1"
set tgtCell to "J2"

set theHtml to do shell script "curl -s " & quoted form of "https://www.google.com/finance/quote/ACS:BME"
set text item delimiters to {"<div class=\"ln0Gge\">", "<div class=\"ln0Gge\">"}
set the extractedDivs to theHtml's text item 1 -- item 2 is the relevant section of the html
set text item delimiters to {"<span>", "</span>"} -- remove the p tags
set tempLst to text items of extractedDivs
set text item delimiters to ""
set tempStr to tempLst as text
set text item delimiters to {"<div class=\"YMLKec fxKbKc\">"}
set theValue to second word of text item 2 of tempStr
-- NO! Don't set the clipboard EVER
-- set the clipboard to theValue
-- return theValue -- uncomment this line for testing -- will appear in Result pane

tell application "Numbers"
     set myDoc to open "/Users/ricardo/Library/Mobile Documents/com~apple~Numbers/Documents/ES Acciones.numbers"
end tell
tell application "Numbers"
	tell table myTableName of sheet mySheetName of myDoc
		set value of cell tgtCell to theValue
	end tell
end tell


Any suggestions? Thanks in advance!


Jul 22, 2021 11:05 AM in response to SonyMiner

Odd. I expected that to work - AppleScript should be aware of international standards for number formats. Mind you, so should Google Finance, and they should use commas if they know that's your standard.


Either way, you can use AppleScript text manipulation to convert the '.' to ','. There are a couple of ways of doing it.


Given:


set theValue to (second word of text item 2 of tempStr)


You can use text item delimiters (safer, but confusing to read):


set {oldTID, my text item delimiters} to {my text item delimiters, "."}
set x to text items of theValue -- this will result in a list like {"22", "13"}
set my text item delimiters to ","
set theValue to x as text -- this converts the list back to text with the current TID
set my text item delimiters to oldTID -- restore state


The other approach is brute force:


set decimalplace to offset of "." in theValue
set theNum to characters 1 through (offset -1) of theValue & "," & characters (offset +1) through end of theValue as text


This brute force approach simply finds the offset of the "." character, then it builds a new string using (character before the ".") plus a comma, plus the characters after the "."


Either of which should end up with a string "22,13" which Numbers.app should accept.


Note that the brute force approach will fail if there's no "." in the string - you might want to add extra checks to the code if this is a problem.

Jul 18, 2021 10:41 AM in response to SonyMiner

When you ask your script to:


> set theValue to second word of text item 2 of tempStr


you are inherently getting a text object as a result - literally a string of characters "2", "2", ".", "1", and "3", so that's what gets transferred to your sheet, and you end up with a text cell.


You can try coercing the result to a numerical object, which should take account of international standards for number formatting and may get you what you want:


set theValue to (second word of text item 2 of tempStr) as number


Failing that you could to a text substitution, but the above should be the safest/most robust way.

Jul 22, 2021 2:23 AM in response to Camelot

Hello,


I thought of making it a number, but I did not know exactly how. I tried your suggestion but I get an error message that is not possible to convert to number:


error "No es posible convertir \"21.04\" en tipo number." number -1700 from "21.04" to number

---Translation (error " Not possible to convert \"21.04\" to number type." number -1700 from "21.04" to number


Maybe this should be a different question posted to the community. My objective now that I was able to grab de web data, is to replace the "." or the "," in the data received.

Jul 23, 2021 8:40 AM in response to SonyMiner

> The second gave me an error saying that there is a parameter missing for offset.


Oops, my bad.


The line should read:


set theNum to characters 1 through (decimalplace -1) of theValue & "," & characters (decimalplace +1) through end of theValue as text


In either case, the text item delimiters is the better approach. Glad it works for you.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to grab web data into Numbers sheet from Google finance

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.