macOS Big-Sur:AppleScript打开网页、读取文本并写入excel文件

macOS Big-Sur:AppleScript打开网页、读取文本并写入excel文件,applescript,Applescript,我开始使用Automator运行一个脚本来打开网页、读取文本和写入excel,但我遇到了一个错误,并在此处发布了一个问题: 现在我发现了如何使用AppleScript进行类似的操作,我的代码如下: to goToWebPage(theWebPage) tell application "Safari" activate set URL of document 1 to theWebPage end tell end goToWebPage goToWebP

我开始使用Automator运行一个脚本来打开网页、读取文本和写入excel,但我遇到了一个错误,并在此处发布了一个问题:

现在我发现了如何使用AppleScript进行类似的操作,我的代码如下:

to goToWebPage(theWebPage)
tell application "Safari"
    activate
    set URL of document 1 to theWebPage
end tell
end goToWebPage

goToWebPage("https://coinmarketcap.com/currencies/bitcoin/markets/")

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

getInputByClass("r", 0)
脚本打开网页,但随后返回错误“变量输入未定义”。为什么脚本没有定义输入?我使用的是多年前在网上找到的锅炉板示例中的错误代码吗?我想打开网页,选择一个每分钟左右都会更改的文本值,然后将其写入excel文件。感谢您的帮助。我是AppleScript和Automator的新手

######################## 编辑: 感谢您的反馈和编辑

这是一个非常简洁的解决方案,谢谢。但我不知道我需要多长时间才能理解它的作用。它还需要写入Excel工作簿,但一旦我有时间研究它使用的高级功能,这是制作精简版本的好方法。尽管到目前为止,与其他语言相比,我更喜欢AppleScript,因为它使用的是更基本的英语命令,可以通过一步一步地阅读代码来解决问题,因此我可以比难以跟踪且无法阅读的速记代码更好地解决错误。但我可以借这个

这是我工作的更新版本。我增加了一些股票,我不介意它使用Safari和JavaScript等计算机应用程序,尽管我希望我可以避免使用Automator,并在excel数据处理和代码编写/学习之间找到如何让它在后台运行和计算的方法。我正在研究如何将其保存为小程序或应用程序,并使用空闲处理程序。您提供的简短解决方案实际上可以与空闲处理程序一起使用。我的没有,因为我认为它有太多的功能。我买了电脑并下载了程序来利用它们,所以如果我牺牲了一小部分计算能力来自动、连续地运行东西,我就不会感到烦恼了。这是我的密码。很抱歉,如果它没有按照您的喜好组织,但我正在学习,代码对我来说是有意义的,我可以把它编写出来,这样我就不介意它对其他人来说是胡言乱语了。除了我在其他论坛上编写的Excel月份条目外,这项功能也可以正常工作。如果有人知道为什么Excel在“(=文本(TayDay.),MMMM”)的中间返回一个@符号SAMAK DAB,当AppleScript将它写入Excel时,我会很欣赏我当前的进展。
--Boiler plate code to manipulate the HTML to let us pull the market price of the stock.--
--3 sets of modifiers for the 3 stocks--
#########################################################
to extractTextBitcoin(searchTextBitcoin, startTextBitcoin, endTextBitcoin)
    set tid to AppleScript's text item delimiters
    set startTextBitcoin to ">"
    set searchTextBitcoin to {"priceValue___11gHJ", 0 & searchTextBitcoin}
    set AppleScript's text item delimiters to startTextBitcoin
    set endItemsBitcoin to text item -1 of searchTextBitcoin
    set AppleScript's text item delimiters to endTextBitcoin
    set beginningToEndBitcoin to text item 1 of endItemsBitcoin
    set AppleScript's text item delimiters to startTextBitcoin
    set endTextBitcoin to (text items 2 thru -1 of beginningToEndBitcoin) as record
    set AppleScript's text item delimiters to tid
end extractTextBitcoin

to extractTextLitecoin(searchTextLitecoin, startTextLitecoin, endTextLitecoin)
    set tid to AppleScript's text item delimiters
    set startTextLitecoin to ">"
    set searchTextLitecoin to {"priceValue___11gHJ", 0 & searchTextLitecoin}
    set AppleScript's text item delimiters to startTextLitecoin
    set endItemsLitecoin to text item -1 of searchTextLitecoin
    set AppleScript's text item delimiters to endTextLitecoin
    set beginningToEndLitecoin to text item 1 of endItemsLitecoin
    set AppleScript's text item delimiters to startTextLitecoin
    set endTextLitecoin to (text items 2 thru -1 of beginningToEndLitecoin) as record
    set AppleScript's text item delimiters to tid
end extractTextLitecoin

to extractTextDogecoin(searchTextDogecoin, startTextDogecoin, endTextDogecoin)
    set tid to AppleScript's text item delimiters
    set startTextDogecoin to ">"
    set searchTextDogecoin to {"priceValue___11gHJ", 0 & searchTextDogecoin}
    set AppleScript's text item delimiters to startTextDogecoin
    set endItemsDogecoin to text item -2 of searchTextDogecoin
    set AppleScript's text item delimiters to endTextDogecoin
    set beginningToEndDogecoin to text item 1 of endItemsDogecoin
    set AppleScript's text item delimiters to startTextDogecoin
    set endTextDogecoin to (text items 2 thru -1 of beginningToEndDogecoin) as record
    set AppleScript's text item delimiters to tid
end extractTextDogecoin
#########################################################
--A tell statement to open the webpage where the stocks are measured--
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/bitcoin/"
end tell

delay 2

--A function that differentiates the data on the web page by class and number. It
--also uses JavaScript to write the data to a useable format.
to getInputByClassBitcoin(theClass, num)
    tell application "Safari"
        set BitcoinInput to do JavaScript "
        document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return BitcoinInput
end getInputByClassBitcoin

--The function with the class and number criteria manually pulled from the web page--
getInputByClassBitcoin("priceValue___11gHJ", 0)

--Setting the instataneous stock price to a variable to input in Excel--
set BitcoinPrice to getInputByClassBitcoin("priceValue___11gHJ", 0)
on BitcoinFunction(BitcoinPrice)
    set BitcoinFunction to extractTextBitcoin(BitcoinPrice, "<div class=>", "</div>")
    return BitcoinFunction(BitcoinPrice)
end BitcoinFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/litecoin/"
end tell

delay 2

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

getInputByClassLitecoin("priceValue___11gHJ", 0)

set LitecoinPrice to getInputByClassLitecoin("priceValue___11gHJ", 0)
on LitecoinFuction(LitecoinPrice)
    set LitecoinFuction to extractTextLitecoin(LitecoinPrice, "<div class=>", "</div>")
    return LitecoinFuction(LitecoinPrice)
end LitecoinFuction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/dogecoin/"
end tell

delay 2

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

getInputByClassDogecoin("priceValue___11gHJ", 0)

set DogecoinPrice to getInputByClassDogecoin("priceValue___11gHJ", 0)
on DogecoinFuction(DogecoinPrice)
    set DogecoinFuction to extractTextDogecoin(DogecoinPrice, "<div class=>", "</div>")
    return DogecoinFuction(DogecoinPrice)
end DogecoinFuction
(*
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Ethereum/"
end tell

delay 2

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

getInputByClassEthereum("priceValue___11gHJ", 0)

set EthereumPrice to getInputByClassEthereum("priceValue___11gHJ", 0)
on EthereumFunction(EthereumPrice)
    set EthereumFunction to extractTextEthereum(EthereumPrice, "<div class=>", "</div>")
    return EthereumFunction(EthereumPrice)
end EthereumFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Binance_coin/"
end tell

delay 2

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

getInputByClassBinance_coin("priceValue___11gHJ", 0)

set Binance_coinPrice to getInputByClassBinance_coin("priceValue___11gHJ", 0)
on Binance_coinFunction(Binance_coinPrice)
    set Binance_coinFunction to extractTextBinance_coin(Binance - coinPrice, "<div class=>", "</div>")
    return Binance_coinFunction(Binance_coinPrice)
end Binance_coinFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Tether/"
end tell

delay 2

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

getInputByClassTether("priceValue___11gHJ", 0)

set TetherPrice to getInputByClassTether("priceValue___11gHJ", 0)
on TetherFunction(TetherPrice)
    set TetherFunction to extractTextTether(TetherPrice, "<div class=>", "</div>")
    return TetherFunction(TetherPrice)
end TetherFunction
#########################################################
tell application "Safari"
    activate
    do shell script "open https://coinmarketcap.com/currencies/Polkadot/"
end tell

delay 2

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

getInputByClassPolkadot("priceValue___11gHJ", 0)

set PolkadotPrice to getInputByClassPolkadot("priceValue___11gHJ", 0)
on PolkadotFunction(PolkadotPrice)
    set PolkadotFunction to extractTextPolkadot(PolkadotPrice, "<div class=>", "</div>")
    return PolkadotFunction(PolkadotPrice)
end PolkadotFunction
#########################################################
*)
--Opens the compiled Excel workbook, negates user input, finds the next available--
--cell to input data, and fills the fields with Year, Month, Day, Time, and Price--
set {year:y, day:d} to (current date)
tell application "Microsoft Excel"
    open "/Users/clusterflux/Desktop/人CRYPTO人_excel.xlsx"
    set display alerts to false
    activate object sheet "ㅇㅅㅇBITCOINㅇㅅㅇ"
    first row index of (get end (last cell of column 9) direction toward the top)
    set LastRowBitcoin to first row index of (get end (last cell of column 9) direction toward the top)
    --write date and time for each market reading to excel file
    set value of cell ("I" & LastRowBitcoin + 1) to y
    set value of cell ("J" & LastRowBitcoin + 1) to ("=TEXT(TODAY(), MMMM)")
    set value of cell ("K" & LastRowBitcoin + 1) to d
    set value of cell ("L" & LastRowBitcoin + 1) to (time string of (current date))
    set value of cell ("M" & LastRowBitcoin + 1) to BitcoinPrice
    activate object sheet "ㅇㅅㅇLITECOINㅇㅅㅇ"
    first row index of (get end (last cell of column 9) direction toward the top)
    set LastRowLitecoin to first row index of (get end (last cell of column 9) direction toward the top)
    set value of cell ("I" & LastRowLitecoin + 1) to y
    set value of cell ("J" & LastRowLitecoin + 1) to ("=TEXT(TODAY(), MMMM)")
    set value of cell ("K" & LastRowLitecoin + 1) to d
    set value of cell ("L" & LastRowLitecoin + 1) to (time string of (current date))
    set value of cell ("M" & LastRowLitecoin + 1) to LitecoinPrice
    activate object sheet "ㅇㅅㅇDOGECOINㅇㅅㅇ"
    first row index of (get end (last cell of column 9) direction toward the top)
    set LastRowDogecoin to first row index of (get end (last cell of column 9) direction toward the top)
    set value of cell ("I" & LastRowDogecoin + 1) to y
    set value of cell ("J" & LastRowDogecoin + 1) to ("=TEXT(TODAY(),  MMMM)")
    set value of cell ("K" & LastRowDogecoin + 1) to d
    set value of cell ("L" & LastRowDogecoin + 1) to (time string of (current date))
    set value of cell ("M" & LastRowDogecoin + 1) to DogecoinPrice
    set workbookName to ("人CRYPTO人_excel.xlsx") as string
    set destinationPath to (path to desktop as text) & workbookName
    save active workbook in destinationPath
    delay 2
    --close every workbook saving no--
    --tell application "Microsoft Excel"--
    --quit--
end tell

set closeURLs to {"https://coinmarketcap.com/currencies/bitcoin/", "https://coinmarketcap.com/currencies/litecoin/", "https://coinmarketcap.com/currencies/dogecoin/"}

repeat with theURL in closeURLs
    tell application "Safari" to close (every tab of every window whose URL contains (contents of theURL))
end repeat
——操纵HTML的锅炉板代码,让我们拉取股票的市场价格--
--3套适用于3种库存的修改器--
#########################################################
提取文本比特币(searchTextBitcoin、startTextBitcoin、endTextBitcoin)
将tid设置为AppleScript的文本项分隔符
将startTextBitcoin设置为“>”
将searchTextBitcoin设置为{“priceValue__11gHJ”,0&searchTextBitcoin}
将AppleScript的文本项分隔符设置为StartText比特币
将endItemsBitcoin设置为searchTextBitcoin的文本项-1
将AppleScript的文本项分隔符设置为endTextBitcoin
将BeginningToEnd比特币设置为endItemsBitcoin的文本项1
将AppleScript的文本项分隔符设置为StartText比特币
将endTextBitcoin设置为(BeginingToEndBitcoin的文本项2到-1)作为记录
将AppleScript的文本项分隔符设置为tid
比特币结束
提取TextLiteCoin(searchTextLitecoin、startTextLitecoin、endTextLitecoin)
将tid设置为AppleScript的文本项分隔符
将startTextLitecoin设置为“>”
将searchTextLitecoin设置为{“priceValue__11gHJ”,0&searchTextLitecoin}
将AppleScript的文本项分隔符设置为startTextLitecoin
将endItemsLitecoin设置为searchTextLitecoin的文本项-1
将AppleScript的文本项分隔符设置为endTextLitecoin
将beginningToEndLitecoin设置为endItemsLitecoin的文本项1
将AppleScript的文本项分隔符设置为startTextLitecoin
将endTextLitecoin设置为(beginningToEndLitecoin的文本项2到-1)作为记录
将AppleScript的文本项分隔符设置为tid
尾币
提取TextDogeCoin(searchTextDogecoin、startTextDogecoin、endTextDogecoin)
将tid设置为AppleScript的文本项分隔符
将startTextDogecoin设置为“>”
将searchTextDogecoin设置为{“priceValue__11gHJ”,0&searchTextDogecoin}
将AppleScript的文本项分隔符设置为startTextDogecoin
将endItemsDogecoin设置为searchTextDogecoin的文本项-2
将AppleScript的文本项分隔符设置为endTextDogecoin
将beginningToEndDogecoin设置为endItemsDogecoin的文本项1
将AppleScript的文本项分隔符设置为startTextDogecoin
将endTextDogecoin设置为(beginningToEndDogecoin的文本项2到-1)作为记录
将AppleScript的文本项分隔符设置为tid
结束提取文本DogeCoin
#########################################################
--一份tell声明,用于打开测量股票的网页--
告诉应用程序“Safari”
激活
是否打开shell脚本https://coinmarketcap.com/currencies/bitcoin/"
结束语
延迟2
--按类别和编号区分网页上数据的函数。信息技术
--还使用JavaScript将数据写入可用格式。
获取InputByClassBitCoin(类,num)
告诉应用程序“Safari”
将BitcoinInput设置为执行JavaScript“
document.getElementsByClassName(“&theClass&”)[“&num&”].innerHTML;”在文档1中
结束语
返回位输入
结束getInputByClassBitcoin
--从网页中手动提取具有类别和编号标准的函数--
getInputByClassBitcoin(“priceValue\uuuuu11ghj”,0)
--将非即时股价设置为要在Excel中输入的变量--
将BitcoinPrice设置为getInputByClassBitcoin(“priceValue\uuuuu11ghj”,0)
关于BitcoinFunction(BitcoinPrice)
将BitcoinFunction设置为extractTextBitcoin(BitcoinPrice,“,”
property eGrepBitcoinPrice : "priceValue___11gHJ\">\\$\\d{2},\\d{3}.\\d{2}"
property currentBitcoinPrice : missing value

set currentBitcoinPrice to do shell script ¬
    "curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
    "| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 21-"