Applescript Question [extracting data from websites]

Discussion in 'Mac Apps and Mac App Store' started by paulmc49255, Oct 20, 2013.

  1. paulmc49255 macrumors newbie

    Oct 20, 2013
    Here is my situation in detail.

    I have figured out how to login to a website, not using javascript, but using "keystrokes", "tabs" and "returns" to find the username and password fields. I know this can be unreliable as websites change but for now I am just trying to keep it simple.

    After I log in to a site, like a bank account, I want to grab the account balance and import it to Numbers.

    I have figured out how to insert a predermined text into a specific cell in Numbers and probably could expand that to dialog boxes ect, but that is not what I am really trying to accomplish.

    Basically I don't know what I need to do to grab the data from the current website after I have logged in. Doing research I realize it is something todo with javascript.

    For now and to apply it to the bigger plan, I just want to know how to locate data of a website that I do not have to log into in Safari via javascript and display the data in the results of Applescript Editor. And I am trying to figure out how I could use that result to display it in another application (ei Numbers). I then would apply this to other sites, so I would like to know exactly where to look to find the data.

    I am thinking that stock websites would be a great example but I am having a tough time finding one that fits this example.

    Thank you for your help!
  2. HexMonkey Administrator


    Staff Member

    Feb 5, 2004
    New Zealand
    You can do this by getting the page's source via AppleScript, and then scanning it for what you're looking for. You'll need to figure out how the particular page is structured to figure out where to find the value(s) you want, but you will normally be able to write some fairly simple code to find what you're looking for.

    Here's a crude example that gets Apple's stock price from Yahoo Finance (based on its current source on my end - no guarantees that this will continue working):

    tell front window of application "Safari"
    	--Create a new page and get its source
    	set theTab to make new tab with properties {URL:""}
    	delay 3 --give the page a bit of time to load (crude method)
    	set theSource to source of theTab
    	--Find the stock price by finding the text between <span id="yfs_l84_aapl"> and </span> in the page's source
    	set stockPrice to my textBetween(theSource, "<span id=\"yfs_l84_aapl\">", "</span>")
    	close theTab
    	return stockPrice
    end tell
    on textBetween(searchText, startText, endText)
    	set defaultDelims to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to startText
    	if (count of text items of searchText) is 1 then return "" -- no match for startText
    	set searchText to second text item of searchText
    	set AppleScript's text item delimiters to endText
    	if (count of text items of searchText) is 1 then return "" -- no match for endText
    	set searchText to first text item of searchText
    	set AppleScript's text item delimiters to defaultDelims
    	return searchText
    end textBetween
    A note of caution: if you are logging into websites using AppleScript, there are likely going to be security implications (especially if you are storing the password in the script itself). Some websites (especially banks) also have conditions that prohibit the use of automated tools to log in and access information.
  3. onekerato macrumors regular

    Jun 6, 2011

Share This Page