编程语言
首页 > 编程语言> > Applescript和Javascript从chrome中获取数字并给出总数

Applescript和Javascript从chrome中获取数字并给出总数

作者:互联网

我正在尝试从内部网页获取每个总数并得到总数.

为了得到一个结果我写了这个:

tell application "Google Chrome"
    tell tab 1 of window 1 to set orderTotal to execute javascript "document.getElementsByClassName('total')[0].innerHTML;"
end tell 

这个例子的输出是:

"<td>1,200.00</td><td>JPY</td>"

(PS货币并不总是一样)

让每一项价值得以实现

tell application "Google Chrome"
    tell tab 1 of window 1 to set r to execute javascript "var outPut=[]; var arr=document.getElementsByClassName('total');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;"
end tell
 return r

给我这样的东西:

"<td>4,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>3,800.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", missing value, missing value, missing 

我想清除文字并做一下,

尝试清除这些值,但显然不起作用:

set theText to Unicode text
property leftEdge1 : "<td>"
property rightEdge1 : "</td>"
try
    set saveTID to text item delimiters
    set text item delimiters to leftEdge1
    set classValue to text item 2 of r
    set text item delimiters to rightEdge1
    set singleValue to text item 1 of classValue
    set text item delimiters to saveTID
    singleValue
end try

总而言之,我还不确定如何继续.

解决方法:

由于缺少实际URL以及OP中显示的不完整输出显示,以下示例AppleScript代码将r定义为列表并对其数值求和.

set r to {"<td>4,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>3,800.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", missing value, missing value, missing value}

set theText to Unicode text
property leftEdge1 : "<td>"
property rightEdge1 : "</td>"
set totalValue to 0

repeat with i from 1 to count r
    try
        set saveTID to text item delimiters
        set text item delimiters to leftEdge1
        set classValue to text item 2 of item i of r
        set text item delimiters to rightEdge1
        set singleValue to text item 1 of classValue
        set text item delimiters to saveTID
        set totalValue to totalValue + singleValue
    end try
end repeat

log totalValue

-- Result:
-- 42800

注意:示例AppleScript代码就是这样,并且不会使用任何其他错误处理然后显示的内容,仅用于显示完成任务的多种方法之一.用户有责任根据需要添加/使用适当的错误处理.

标签:javascript,google-chrome,applescript,javascript-automation
来源: https://codeday.me/bug/20190819/1694394.html