How to grab web data into Numbers sheet

Hello, I am trying to grab the value out of a webpage to paste ir in the Numbers sheet that I am using.

The data is the "Valor Liq." value in the site. Located in the top table.

https://www.bolsasymercados.es/MTF_Equity/esp/SICAV/Ficha/ACATES_SISAPON__SICAV__S_A__ES0176200030.aspx


I found this script from a previous question, but not able to adapt it get what I want.


-- change values between "" in first three lines to match your setup

set mySheetName to "Prices"

set myTableName to "Coin Prices"

set tgtCell to "A2"


set theHtml to do shell script "curl -s " & quoted form of "https://www.worldcoinindex.com"

set text item delimiters to {"<tbody>", "</tbody>"}

set tableContents to theHtml'stext item 2 # item 2 is the body of the price table

set text item delimiters to {"<h2>"} # site uses new h2 for each currency

set tableChunks to tableContents'stext items 2 thru -1

set pasteStr to ""

repeat with aChunk in tableChunks

set text item delimiters to "><span>$ </span><span class=\"span\">"

tell aChunk's text item 1 to set {theSymbol, thePrice} to {first word, last word}

set pasteStr to pasteStr & theSymbol & tab & thePrice & return

end repeat

set the clipboard topasteStr


tell application "Numbers"

tell front document

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

tell sheetmySheetName to tell tablemyTableName


activate

set selection range to rangetgtCell

delay 0.3

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

end tell


activate

set active sheet to sheetcurrSheet# go back to original location

end tell

end tell



Any ideas how to grad the data?


Screenshot of the data:

MacBook Pro 13″, macOS 11.4

Posted on May 27, 2021 4:03 AM

Reply
Question marked as Top-ranking reply

Posted on May 27, 2021 4:46 AM

If you want just that one value then you can do something like this:


-- change values between "" in first three lines to match your setup
set mySheetName to "MySheetName"
set myTableName to "MyTableName"
set tgtCell to "B2"

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"
	tell front document
		tell active sheet to set currSheet to its name # save current location
		tell sheetmySheetName to tell tablemyTableName
			activate
			set selection range to rangetgtCell
			delay 0.3
			tell application "System Events" to keystroke "v" using {option down, shift down, command down}
		end tell
		
		activate
		set active sheet to sheetcurrSheet # go back to original location
	end tell
end tell


This assumes you have changed the first three lines of the script to match your setup. Note that the value is not placed in a Numbers "sheet" but in a table. A sheet in Numbers has a blank canvas on which you place tables, charts and other objects.


SG

Similar questions

5 replies
Question marked as Top-ranking reply

May 27, 2021 4:46 AM in response to SonyMiner

If you want just that one value then you can do something like this:


-- change values between "" in first three lines to match your setup
set mySheetName to "MySheetName"
set myTableName to "MyTableName"
set tgtCell to "B2"

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"
	tell front document
		tell active sheet to set currSheet to its name # save current location
		tell sheetmySheetName to tell tablemyTableName
			activate
			set selection range to rangetgtCell
			delay 0.3
			tell application "System Events" to keystroke "v" using {option down, shift down, command down}
		end tell
		
		activate
		set active sheet to sheetcurrSheet # go back to original location
	end tell
end tell


This assumes you have changed the first three lines of the script to match your setup. Note that the value is not placed in a Numbers "sheet" but in a table. A sheet in Numbers has a blank canvas on which you place tables, charts and other objects.


SG

May 30, 2021 2:21 AM in response to SonyMiner

The general approach is to look at the source html for the page. In Safari enable the Develop menu (Safari > Preferences > Advanced and check 'Show Develop menu bar'). Then to see the source for a webpage go in the menu to Develop > Show Page Source.


For many (but not all) pages you can locate in the html the value that you are looking for. Then you study the text around the value and figure out how to use text item delimiters to chunk the html to get what you want.


This is a very basic form of web scraping. There are many other more sophisticated tools, such as Beautiful Soup under Python.


But good old humble AppleScript can get the job done in many situations.


Many thanks for the green tick!


SG




SG

May 29, 2021 3:18 AM in response to SGIII

Wow! Thanks a lot. Works perfectly!!!


And to make it even better, If I wanted to get data from other webs, could I include it in the same script? Repeating the process with a new set of sheet and table names?


Another question that comes to mind, is it possible to run it as I open the numbers file? So I am guessing that by duplicating the same script with different parameters one after the last "end tell" it should work, correct?


I tried a new data grab, but I am getting the wrong result, I get "<" instead of the price. I understand the logic, but having trouble figuring out the correct path and data location.


This is what Ia have for quoting (as a sample) SONY in: https://es.investing.com/equities/sony


-- change values between "" in first three lines to match your setup

set mySheetName to "MySheetName"

set myTableName to "MyTableName"

set tgtCell to "B2"


set theHtml to do shell script "curl -s " & quoted form of "https://es.investing.com/equities/sony"


set text item delimiters to {"<div class=\"instrument-price_instrument-icon__3zhqL instrument-price_up__2-OcT\">", "<div class=\"instrument-price_change__3UTmm\">"}


set the extractedDivs to theHtml's text item 3 -- 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=\"instrument-price_last__KQzyA\" data-test=\"instrument-price-last\">"}

set theValue to first word of text item 1 of tempStr

set the clipboard to theValue

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

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


What is wrong? Thanks in advance...

May 30, 2021 1:35 AM in response to SGIII

I imagine webs change, but it was interesting to be able to get data, specially real time data. I think is too bad Numbers does include a tool to grab data from the web, even if it is not perfect, since most data now days is on the web. I think it would a good argument for people to move from excel to numbers.


Thanks for your help, I will try some more to see if I can get what I want...


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

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