To getInputB圜lass(theClass, num) - defines a function with two inputs, theClass and num Use AppleScript version “2.4” - Yosemite (10.10) or later Any thoughts? Here’s the entire AppleScript document: However if I manually call getElementsByTagName with the appropriate tag and index in Chrome’s Javascript console, I get exactly what I’m looking for. When I step through the code, it looks Safari isn’t finding any elements with the classname. I’m new to javascript and am having trouble getting this code to work on El Capitan & Chrome 52. To getInputByName ( theName, num ) - defines a function with two inputs, theName and numĭocument.getElementsByName('" & theName & "').innerHTML " in document 1 Tell application "Safari" -tells AS that we are going to use Safariĭocument.getElementsByTagName('" & the Tag & "').innerHTML " in document 1 To getInputBy Tag ( the Tag, num ) - defines a function with two inputs, the Tag and num TogetInputById (theId ) tell application "Safari"ĭocument.getElementById('" & theId & "').innerHTML " in document 1Įnd tell return input endgetInputById Grabbing Data from a Website Using Tag If you read the previous tutorials mentioned at the beginning of this article you will understand how to use the below code. See below for how to use other methods to grab data. Now we enter this into our AppleScript… set theResult to extractText (theText, " ">", " " ) Which comes directly after the information that we want. We are going to set startText to “> which is the last part of: We want to extract “The correct way to peel a banana – Instructables”, which is between “The correct way to peel a banana – Instructables” This is the result of our getInputB圜lass function: In order to get this we need to look at our code for what comes right before the information we want to extract and what comes directly after. The first is searchText, this is going to be what we retrieved from our getInputB圜lass Function, or theText above. First we need to set our grabbed text to a variable. We can set up this function to pull out what is between the lines of code. SetAppleScript 's text item delimiters to startText2 set finalText to ( text items 2 thru -1 of beginningToEnd) as text setAppleScript 's text item delimiters to tid returnfinalText SetAppleScript 's text item delimiters to endText setbeginningToEnd to text item 1 ofendItems Set searchText to ("x" & searchText ) setAppleScript 's text item delimiters to startText1 setendItems to text item -1 ofSearchText To get rid of the HTML we are going to use AppleScripts Text Delimiter functions.Įnter the following code into the top of your AppleScript doc… to extractText (searchText, startText2, endText ) set tid toAppleScript 's text item delimiters Hmm, this is good that we have the information that we want, but we also picked up a lot of the HTML. The first result would be 0, the second 1, third 2, etc… In this instance the 0 would allude to which headline we would like to pull. Enter the following code in your AppleScript doc to get the data… Now that we have our data scraper function we can take our first stab at pulling the info. TogetInputB圜lass (theClass, num ) - defines a function with two inputs, theClass and num tell application "Safari" -tells AS that we are going to use Safariĭocument.getElementsB圜lassName('" & theClass & "').innerHTML " in document 1 - uses JavaScript to set the variable input to the information we wantĮnd tell return input -tells the function to return the value of the variable input endgetInputB圜lass Grabbing Data from a Website Using Classįirst, paste this code into the top of your AppleScript Doc… It looks like we do not have an ID or Name to go off of, so we will have to use Class. If you are following along the code should look something like this… Right click on the element you want to grab and click on inspect element to bring up the source code. We are going to grab the first headline of how to peel a banana and pull it into applescript. Please view these first!Īs in the previous examples we are going to need either an ID, Class, Name, or if all else fails a the tag that contains the information that we want.įirst go to the web page that you would like to grab information from… If have not read my previous two tutorials on clicking and inputting data, this will not make much sense. In a later tutorial I will teach how to put all of this information together to make a fully automated application that collects and/or inputs multiple bits of data from a website or websites.įor this first example we are going to use Google Chrome’s inspect element tool and grab the first line of a Google search result. Today we are going to learn how to extract data from web pages using AppleScript! In previous tutorials we learned how to make applescript open a web page, how to use AppleScript to fill out forms on a web page, and how to click buttons on web pages with AppleScript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |