【问题标题】:AppleScript Text delimiters "Can’t get text items"AppleScript 文本分隔符“无法获取文本项”
【发布时间】: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


【解决方案1】:

常见的解决方案,它还会检查源文本是否包含两个标签

set sourceText to "<td class=\"inspectDATAInInspector\"><a href=\"/WebObjects/DATA.DATA/DT/DDDDTTTAAAADDTA/0.1.0\"></a>48784745</td>"

set startTextAfterTag to "</a>"
set endTextBeforeTag to "</td>"

set startOffset to offset of startTextAfterTag in sourceText
set endOffset to offset of endTextBeforeTag in sourceText
if startOffset = 0 or endOffset = 0 or endOffset < startOffset then
    display dialog "The source text does not contain the specified tags."
    return
end if

set extractedText to extractTextBetweenTags(sourceText, startTextAfterTag, endTextBeforeTag)

on extractTextBetweenTags(theText, startTag, endTag)
    set saveTID to text item delimiters
    set text item delimiters to startTag
    set secondPart to text item 2 of theText
    set text item delimiters to endTag
    set firstPart to text item 1 of secondPart
    set text item delimiters to saveTID
    return firstPart
end extractTextBetweenTags

编辑:

建议 #2:它捕获倒数第二个 &gt;&lt;/td 标记之间的所有内容

set sourceText to "<td class=\"inspectDATAInInspector\"><a href=\"/WebObjects/DATA.DATA/DT/DDDDTTTAAAADDTA/0.1.0\"></a>48784745</td>"

set startTextAfterTag to ">"
set endTextBeforeTag to "</td"

set extractedText to extractTextBetweenTags(sourceText, startTextAfterTag, endTextBeforeTag)

on extractTextBetweenTags(theText, startTag, endTag)
    set saveTID to text item delimiters
    set text item delimiters to startTag
    set secondPart to text item -2 of theText
    set text item delimiters to endTag
    set firstPart to text item 1 of secondPart
    set text item delimiters to saveTID
    return firstPart
end extractTextBetweenTags

建议#3:如果你安装了SatImage.OSAX ,你可以使用正则表达式

set sourceText to "<td class=\"inspectDATAInInspector\"><a href=\"/WebObjects/DATA.DATA/DT/DDDDTTTAAAADDTA/0.1.0\"></a>48784745</td>"

try
    set foundText to find text ">(\\d+)</td>$" in sourceText using 1 with regexp
    set extractedText to foundText's matchResult
on error
    display dialog "The source text does not match the regex."
end try

【讨论】:

  • 对不起,我没有正确解释自己。这是 HTML 数据,我想得到“48784745”,但“48784745”甚至“WebObjects/DATA.DATA/DT/DDDDTTTAAAADDTA/0.1.0\">”总是在改变我想要获取的数据. 没有改变的是 "
  • 我编辑了答案,提供了另外两个建议。最后一个需要SatImage.osax
猜你喜欢
相关资源
最近更新 更多
热门标签