Я пытаюсь заставить AppleScript найти некоторые данные с веб-сайта и скопировать их в виде текста.Разделители текста в AppleScript «Не удается получить текстовые элементы»
Однако когда-нибудь у меня есть ошибка: ошибка «Не удается получить текстовые объекты от 2 до -1 из« [email protected] \ ».» номер -1728 из текстовых элементов от 2 до -1 из "[email protected]"
Есть идеи?
to getInputByClass(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 getInputByClass
-- start here
getInputByClass("spacebefore", 0)
set theText to getInputByClass("spacebefore", 0)
-- clear text
set DATA1ID to extractText(theText, "\">", "</td>")
to extractText(searchText, startText2, endText)
set tid to AppleScript's text item delimiters
set startText1 to "x"
set searchText to ("x" & searchText)
set AppleScript's text item delimiters to startText1
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText2
set finalText to (text items 2 thru -1 of beginningToEnd)
set AppleScript's text item delimiters to tid
return finalText
end extractText
-- DATA1ID Found
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("inspectDSInInspector", 0)
set theText to getInputByClass2("inspectDSInInspector", 0)
-- clear text
set DATA2DSID to extractText2(theText, "</a>", "</td>")
to extractText2(searchText, startText2, endText)
set tid to AppleScript's text item delimiters
set startText1 to "x"
set searchText to ("x" & searchText)
set AppleScript's text item delimiters to startText1
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText2
set finalText to (text items 2 thru -1 of beginningToEnd)
set AppleScript's text item delimiters to tid
return finalText
end extractText2
set finalResult to "DATA2DSID: " & DATA2DSID & "
DATA1ID: " & DATA1ID
set the clipboard to finalResult
tell application "System Events" to keystroke "v" using command down
UPDATE:
<td class="inspectDATAInInspector"><a href="/WebObjects/DATA.DATA/DT/DDDDTTTAAAADDTA/0.1.0"></a>48784745</td>
"HREF ="/WebObjects/DATA.DATA/DT/DDDDTTTAAAADDTA/0.1.0 "> 48784745" не исправить данные, что не будет меняться, и в чем я нуждаюсь, это случайное число в конце, в этом случае 48784745
Сценарий, который я здесь сделал, работает здесь, но время от времени у меня есть упомянутое сообщение. Я думаю, что это может быть потому, что я должен обмениваться данными с текстом Plain до HTML или что-то в этом роде.
Не имея исходный текст это невозможно дать руку. По крайней мере, сообщение об ошибке говорит, что количество текстовых элементов 'beginToEnd' меньше 2. – vadian
Это на самом деле веб-база данных, было бы это потому, что я должен установить переменную как« как текст »? –
Извините, я понятия не имею. Parsing html очень специфичен в зависимости от исходного текста. – vadian