Get value from Table recovered from the WEB

Trying to streamline some code that gets values from a table on the web by downloading the table rather than getting the value directly from the web as some times the table on the web is quite large. The following script certainly gets the table as it can count its rows and columns. I can and do get the results I need by scrolling through the table online, I was hoping to improve on that . Thanks for any suggestions.

tell application “System Events” to tell process “Safari”

set ty to table 1 of group 24 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1

set Ry to count every row of ty

set Cy to count every column of ty

repeat with R from 1 to Ry

repeat with C from 1 to Cy

–log value of item R of row Ry of item C of column C of Cy

log item C of column Cy of item R of row R of Ry of table ty

end repeat

end repeat

end tell

Can you post an URL so one has a chance to reproduce what you’re doing?

Thanks for getting back to me . The source for what I am doing is finance.yahoo and this is a URL for a stock Federal Signal Corporation (FSS) Options Chain - Yahoo Finance. that should bring up the website for “Federal Signal” and you will see in the bottom half two tables and as you can see from my code I am accessing the 1st one, hope this helps

missed the url

Please learn how to post code properly.

property header : {}
property mytable : {}

tell application "System Events"
	tell application process "Safari"
		set ty to table 1 of group 22 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
		set Ry to count rows of ty
		set Cy to count columns of ty
		repeat with C from 1 to Cy
			set end of header to (value of UI element 1 of UI element C of row 1 of ty)
		end repeat
		repeat with R from 2 to Ry
			set tmpRec to {}
			repeat with C from 1 to Cy
				if (role of UI element 1 of UI element C of row R of ty) = "AXLink" then
					set end of tmpRec to (title of UI element 1 of UI element C of row R of ty)
				else
					set end of tmpRec to (value of UI element 1 of UI element C of row R of ty)
				end if
			end repeat
			set end of mytable to tmpRec
		end repeat
	end tell
end tell

From your sample WebLink, its should be group 22

Thank you the correct group is 24 not 22, and I apologise for not posting my code correctly , I am sorry but I just have not been able to figure that out since the site changed

Weird, I cant get group 24 to work, but 22 works fine

Is it the “PUTS” table or the “CALL” table?

Also please learn how to post code so that the “Open in Script Editor” button shows up and also the code doesn’t get it’s quote character replaced with typographer quotes.

It’s the call table.

I understand that I need to learn but I do not know how to most so “Open in Script Editor” button shows up.

The following works with Chrome

tell application "Google Chrome"
	set js to "result=''; 
  var t = document.getElementsByTagName('table');
  var rows=t[0].querySelectorAll('tbody>tr'); 
  [...rows].forEach(r => {
    const cells=r.getElementsByTagName('td');
   result += [...cells].map(c => c.innerText).join(',')
  });
result;
"
	tell tab 1 of window 1 to set result to execute javascript (js as string)
	log result
end tell

It injects JavaScript into the browser (using execute). The JavaScript uses the obvious DOM methods to get all cells of the first table (t[0]) by first retrieving all table rows in the table body (querySelectorAll('tbody > tr')) and then looping over the rows and getting the innerText of all their td elements.

These innerText values are returned as a single string, separated by commas.
This is obviously a completely different approach from yours, but it is what one does in general to scrape web content. Using UI scripting is error-prone and might fail with currently invisible stuff, as you found out.

To post code, you should include it in three backquotes like so:
```applescript
code goes here
```
I’m not sure if the “applescript” language identifier is needed here, but including it shouldn’t do any harm.

To have the same thing happen in Safari, use tell application "Safari" at the top and
set result to do JavaScript js in tab 1 of window 1 instead of the line tell tab 1…
It is not possible to do all this with Firefox since its scripting support is still in its infancy – since over 20 years.

                                                                               k
Summary

This text will be hidden

Still group 22 for me.

Maybe it’s different if you are logged-in.
Since I am not, while using the link you gave

At some point for me group 22 became group 23. I guess it takes a while for everything to finish loading. With group 23, I ended up with a list of the three records in the Calls table.

Thank you; unfortunately know nothing about Python and very little about Javascript and the robertfern solution works

Peter

You can get table data by using “HTMLReader” framework.

And it is eay to use from AppleScript.

http://piyocast.com/as/archives/7469