使用Applescript在两个常量之间查找变量文本

时间:2013-09-05 09:56:12

标签: javascript string applescript webpage

我是Apple的新手。我试图通过safari从网页返回一个字符串变量。目标字符串更改,但前后的单词是静态的。

示例:Apple 1293.34 USD

“Apple”和“USD”永远不会改变,但我想要的只是两者之间的数字。

该元素没有ID,我没有运气试图通过Javascript上课。

谢谢:)

3 个答案:

答案 0 :(得分:0)

您应该首先找到您的元素。

var element = document.getElementById("elementID");

然后得到你的价值:

 var value = parseFloat(element.innerText.split(" ")[1])

答案 1 :(得分:0)

您可以尝试解析Curl ...

set myUrl to "http://stackoverflow.com/questions/18633200/finding-variable-text-between-two-constants-with-applescript"

set xxx to do shell script "curl " & quoted form of myUrl & " | grep -o Apple.*USD | sed 's/Apple\\(.*\\)USD/\\1/'"

答案 2 :(得分:0)

这是给出文本的AppleScript方法。这并不是假设Apple只在文本中找到一次。这将查看并查找Apple后来跟随2个单词的位置,然后在它们之间获得正确的值。

set theText to "USD some words Apple USD more words Apple 1293.34 USD some words Apple USD"

set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "Apple"}
set textItems to text items of theText
set AppleScript's text item delimiters to tids

set theValue to missing value
repeat with i from 2 to (count of textItems)
    set thisItem to item i of textItems
    try
        if word 2 of thisItem is "USD" then
            set theValue to word 1 of thisItem
            exit repeat
        end if
    end try
end repeat

return theValue