Kevin
Kevin

Reputation: 1106

Getting text from webpage

I asked and read different topic about this in the last month, but still didn't get it

I want to extract some data from a website using Applescript or something simular

With this script

to getInputByClass2(theClass, num) -- defines a function with two inputs, theClass and num
    tell application "Safari" --tells AS that we are going to use Safari
        set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 -- uses JavaScript to set the variable input to the information we want
    end tell
    return input --tells the function to return the value of the variable input
end getInputByClass2


-- start here
getInputByClass2("field type-string field-accountname", 0)
set theText to Unicode text
set theText to getInputByClass2("field type-string field-accountname", 0)

I have this result :

"<div class=\"content\"><div class=\"related-orders-indicator component\" id=\"XXXX-BBBB-CCCC-DDDD-EEEE\" data-bad=\"0\" data-good=\"51\" data-total=\"51\">
    <label class=\"count\" style=\"display: block;\">51</label>
</div><label class=\"name\">accountname</label><span class=\"value\">1985</span><div class=\"confirmation\"><input type=\"checkbox\" class=\"tri-state confirmation square-confirmation-style\"></div></div>"

How can I have only the account name as a variable, in this example 1985.

Many thanks in advance

Upvotes: 0

Views: 732

Answers (1)

vadian
vadian

Reputation: 285069

You can do it with text item delimiters:

property leftEdge : "class=\"value\">"
property rightEdge : "</span>"

set theSource to "<div class=\"content\"><div class=\"related-orders-indicator component\" id=\"XXXX-BBBB-CCCC-DDDD-EEEE\" data-bad=\"0\" data-good=\"51\" data-total=\"51\"><label class=\"count\" style=\"display: block;\">51</label></div><label class=\"name\">accountname</label><span class=\"value\">1985</span><div class=\"confirmation\"><input type=\"checkbox\" class=\"tri-state confirmation square-confirmation-style\"></div></div>"

set saveTID to text item delimiters
set text item delimiters to leftEdge
set classValue to text item 2 of theSource
set text item delimiters to rightEdge
set theResult to text item 1 of classValue
set text item delimiters to saveTID
theResult

Upvotes: 1

Related Questions