Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AppleScript:如何从字符串中提取数字?_Applescript - Fatal编程技术网

AppleScript:如何从字符串中提取数字?

AppleScript:如何从字符串中提取数字?,applescript,Applescript,我正在写一个脚本去纽约时报在科罗纳的网站,获取美国的数据,提取数字(总数,死亡),并给我发送通知。我很接近,但当我提取数字并显示它们时,它们被放在一起(即700021而不是7000,21)。我的问题是: 如何提取这些数字,以便对其进行描述 代码如下: set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&stat

我正在写一个脚本去纽约时报在科罗纳的网站,获取美国的数据,提取数字(总数,死亡),并给我发送通知。我很接近,但当我提取数字并显示它们时,它们被放在一起(即700021而不是7000,21)。我的问题是:

如何提取这些数字,以便对其进行描述

代码如下:

set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show&region=TOP_BANNER&context=storyline_menu"

tell application "Safari" to make new document with properties {URL:theURL}

tell application "System Events"
    repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
        delay 0.5
    end repeat
end tell

to getInputByClass(theClass, num)
    tell application "Safari"
        set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerText;" in document 1
    end tell
    return input
end getInputByClass

set myVar to getInputByClass("g-body ", 5)

on returnNumbersInString(inputString)
    set s to quoted form of inputString
    do shell script "sed s/[a-zA-Z\\']//g <<< " & s
    set dx to the result
    set numlist to {}
    repeat with i from 1 to count of words in dx
        set this_item to word i of dx
        try
            set this_item to this_item as number
            set the end of numlist to this_item
        end try
    end repeat
    return numlist
end returnNumbersInString

set theNums to returnNumbersInString(myVar) as text

display notification "COVID-19 UPDATE" subtitle theNums sound name "glass"

tell application "Safari"
    close its front window
end tell
将URL设置为“https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-冠状病毒&variant=show®ion=TOP\u BANNER&context=storyline\u菜单”
告诉应用程序“Safari”创建属性为{URL:theURL}的新文档
告诉应用程序“系统事件”
重复此操作,直到存在(应用程序进程“Safari”窗口1的工具栏1组的UI元素,其名称=“重新加载此页面”)
延迟0.5
结束重复
结束语
获取InputByClass(类,num)
告诉应用程序“Safari”
将输入设置为执行JavaScript“
document.getElementsByClassName(“&theClass&”)[“&num&”].innerText;”在文档1中
结束语
返回输入
结束getInputByClass
将myVar设置为getInputByClass(“g-body”,5)
关于ReturnNumber安装(inputString)
将s设置为输入字符串的引用形式

执行shell脚本“sed s/[a-zA-Z\\\']//g您将从
returnNumbersInstalling
处理程序中获得一个数字列表,但仅将列表强制为文本通常不会提供任何格式。一种解决方案是使用
文本项分隔符来指定加入列表项时要使用的文本。例如,当转换为notif的文本时你可以这样做:

set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set theNums to returnNumbersInString(myVar) as text
set AppleScript's text item delimiters to tempTID

与我帮助您回答的另一个问题类似,目标数据已经在一个表中,因此我将使用表数据来获取信息,因为它的结构布局不太可能改变,而
5的目标
'g-body'
可能并不总是美国

我获取数据的方式略有不同:

set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show&region=TOP_BANNER&context=storyline_menu"

tell application "Safari" to make new document with properties {URL:theURL}

tell application "System Events"
    repeat until exists ¬
        (UI elements of groups of toolbar 1 of window 1 of ¬
            application process "Safari" whose name = "Reload this page")
        delay 0.5
    end repeat
end tell

tell application "Safari" to tell document 1 to set CountriesTable to ¬
    do JavaScript "document.getElementsByClassName('svelte-f9sygj')[0].innerText;"

tell application "Safari" to close its front window

set awkCommand to ¬
    "awk '/United States/{print $3,\"Cases &\",$4,\"Deaths\"}'"

set notificationMessage to ¬
    do shell script awkCommand & "<<<" & CountriesTable's quoted form

display notification notificationMessage subtitle "US COVID-19 UPDATE" sound name "glass"
将URL设置为“https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-冠状病毒&variant=show®ion=TOP\u BANNER&context=storyline\u菜单”
告诉应用程序“Safari”创建属性为{URL:theURL}的新文档
告诉应用程序“系统事件”
重复此步骤,直到存在为止
(窗口1的工具栏1组的UI元素)
应用程序进程“Safari”,其名称=“重新加载此页面”)
延迟0.5
结束重复
结束语
告诉应用程序“Safari”告诉文档1将CountriesTable设置为
do JavaScript“document.getElementsByClassName('svelte-f9sygj')[0].innerText;"
告诉应用程序“Safari”关闭其前窗口
将awkCommand设置为——
“awk'/美国/{打印$3,\“案例&\”,$4,\“死亡”}”
将notificationMessage设置为——

执行shell脚本awkCommand&“Damnit。。我更喜欢你的。在30多岁的时候学习编码/脚本编写,作为一种在12小时慢班次中娱乐自己的方式,是很困难的。你很有天赋!非常感谢。