私はアップルスクリプトが初めてです。サファリ経由で Web ページから変数に文字列を返そうとしています。ターゲット文字列は変化しますが、前後の単語は静的です。
例: アップル 1293.34 USD
「Apple」と「USD」は決して変わりませんが、私が欲しいのはその間の数字だけです。
要素には ID がなく、Javascript でクラスごとに取得しようとしてもうまくいきませんでした。
ありがとう :)
私はアップルスクリプトが初めてです。サファリ経由で Web ページから変数に文字列を返そうとしています。ターゲット文字列は変化しますが、前後の単語は静的です。
例: アップル 1293.34 USD
「Apple」と「USD」は決して変わりませんが、私が欲しいのはその間の数字だけです。
要素には ID がなく、Javascript でクラスごとに取得しようとしてもうまくいきませんでした。
ありがとう :)
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/'"
最初に要素を見つける必要があります。
var element = document.getElementById("elementID");
次に、値を取得します。
var value = parseFloat(element.innerText.split(" ")[1])
テキストが与えられた場合のapplescriptメソッドは次のとおりです。これは、Apple がテキスト内で 1 回しか見つからないことを前提としていません。これは、Apple の 2 単語後に USD が続いている場所を調べて見つけ、それらの間の適切な値を取得します。
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