【发布时间】:2016-04-09 15:07:35
【问题描述】:
我正在尝试让 AppleScript 从网站上查找一些数据并将它们复制为文本。
但有时我会遇到错误:错误“无法获取 \"correct@data.com\" 的文本项 2 到 -1。” “correct@data.com”的文本项 2 到 -1 中的数字 -1728
有什么想法吗?
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
更新:
<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
我在这里制作的脚本在这里工作,但有时我会提到消息。我认为这可能是因为我必须将数据转换为纯文本,直到 HTML 或类似的东西。
【问题讨论】:
-
没有源文本就不可能提供帮助。至少错误信息说
beginningToEnd的文本项数小于2。 -
它实际上是一个网络数据库,是不是因为我必须将变量设置为“作为文本”?
-
对不起,我不知道。解析 html 非常具体,具体取决于源文本。
标签: text applescript delimiter