由于我没有访问https://www.worldcoinindex.com/watchlist 的帐户并在登录时查看它源代码,我相信它有<tbody> 和</tbody> tags 并为您提供使用 curl 的替代解决方案。
假设您正在使用 Safari 并在目标 URL 登录并且 页面 已完全加载,您可以使用以下 example AppleScript code 来获取您寻找的数据。
将以下内容添加到现有 AppleScript script 的顶部,同时注释掉 code 的 set theHtml to do shell script ... 行。
tell application "Safari" to ¬
set theHtml to do JavaScript ¬
"document.getElementById('myTable').innerHTML;" in document 1
请注意,JavaScript command 中的myTable 来自主域上的table,可能需要针对关注列表。
查看页面来源,例如:
<table id="myTable" class= ... >
<thead>
你也可以使用例如:
"document.getElementsByClassName('...')[0].innerHTML;"in document 1
将... 替换为class= 的源代码中所示
更新:
这是一个版本的示例 AppleScript 代码,它将打开一个新的Safari 文档 到目标URL,然后在后台动态创建一个Numbers document,完成后把它放在最前面。不使用curl 或解析HTML 的方式确实不应该放在首位。没有剪贴板或粘贴到数字。
请注意,一旦确定了id,Safari 窗口 可以保持背景,而一旦窗口首先出现,然后您可以在 脚本 运行时将焦点设置在其他地方。新的 Safari window 在创建时实际上已经在后台,因为 Safari 没有被告知activate。
我为网站创建了一个登录,并将前三个硬币添加到我的关注列表中,这个截图是动态创建的数字 文档。如果您在 example AppleScript 代码。
请注意,按照当前编码,如果未登录,它将通知您并中止 脚本 的运行。我希望稍后更新 code 以处理未登录并在未登录时动态登录,但这是 example AppleScript 的下一次迭代 代码。
示例 AppleScript 代码:
property theURL : "https://www.worldcoinindex.com/watchlist"
-- property theURL : "https://www.worldcoinindex.com"
property myNumbersSheetName : "Coin Prices"
property myNumbersTableName : "Coin Prices"
-- # Do not modify code below unless necessary.
property winID : missing value
property itemCount : missing value
property loginStatus : missing value
property thisNumbersDocument : missing value
property theNumbersDocumentName : missing value
property theSafariDocumentName : missing value
-- # Create a new Safari document to the target URL.
-- # Get the id of the newly created window.
-- # Wait for the page to finish loading.
-- # Get the name of the newly created document.
-- # Get Login status and if not already logged in,
-- # notify user and abort the running of the script.
-- # Get the count of ticker symbols for Numbers.
tell application "Safari"
make new document with properties {URL:theURL}
set winID to id of window 1
my waitForSafariPageToFinishLoading()
set theSafariDocumentName to name of window id winID
tell document theSafariDocumentName
set loginStatus to ¬
do JavaScript ¬
"document.getElementsByClassName('logout-nav-container')[0].innerHTML;"
if loginStatus contains "Login" then
display dialog ¬
"You are not logged in! Please login, " & ¬
"then run script again..." buttons {"OK"} ¬
default button 1 with title ¬
"Login Required To Run This Script"
return
else
set itemCount to my getTickerSymbolCount()
end if
end tell
end tell
-- # Create a new document in Numbers in the background.
-- # Create two columns and one row more than the number of
-- # ticker symbols on the page. Set the column header names.
tell application "Numbers"
set columnCount to 2
set rowCount to itemCount + 1
set thisNumbersDocument to make new document
set theNumbersDocumentName to the name of thisNumbersDocument
tell thisNumbersDocument
delete every table of every sheet
tell active sheet to set its name to myNumbersSheetName
tell sheet myNumbersSheetName
set thisTable to ¬
make new table with properties ¬
{name:myNumbersTableName ¬
, column count:columnCount ¬
, row count:rowCount}
tell thisTable
set value of cell "A1" to "Ticker Symbol"
set value of cell "B1" to "Last Price"
end tell
end tell
end tell
end tell
-- # Get the 'Ticker Symbol' and 'Last Price' for
-- # the number of symbols on the page, setting their
-- # values to the target cells in the Numbers document.
tell application "Safari"
tell document theSafariDocumentName
set n to 2
repeat with i from 0 to itemCount - 1
set |Ticker Symbol| to ¬
first paragraph of ¬
(do JavaScript ¬
"document.getElementsByClassName('ticker')[" & i & "].innerText;")
my addToNumbersTable("A", n, |Ticker Symbol|)
set |Last Price| to ¬
(do JavaScript ¬
"document.getElementsByClassName('number pricekoers lastprice')[" & i & "].innerText;")
my addToNumbersTable("B", n, |Last Price|)
set n to n + 1
end repeat
end tell
end tell
-- # Set focus to cell A1 and bring
-- # the Numbers document frontmost.
tell application "Numbers"
-- # Set focus to cell A1.
tell table myNumbersTableName of ¬
sheet myNumbersSheetName of ¬
document theNumbersDocumentName to ¬
set selection range to range "A1"
activate
end tell
-- ## Handlers ##
to getTickerSymbolCount()
tell application "Safari" to ¬
tell document ¬
theSafariDocumentName to ¬
return ¬
(do JavaScript ¬
"document.getElementsByClassName('ticker').length;") ¬
as integer
end getTickerSymbolCount
to addToNumbersTable(c, n, v)
-- # Sets the value of the target cell.
tell application "Numbers" to ¬
tell table myNumbersTableName of ¬
sheet myNumbersSheetName of ¬
document theNumbersDocumentName to ¬
set value of cell (c & n) to v
end addToNumbersTable
on waitForSafariPageToFinishLoading()
-- # Wait for page to finish loading in Safari.
-- # This works in **macOS Catalina** (10.15.7) and
-- # macOS Big Sur (11.4) and may need adjusting for
-- # updated versions of Safari in these version of
-- # macOS, or other versions of macOS past or future.
tell application "System Events" to repeat until ¬
exists (buttons of groups of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end waitForSafariPageToFinishLoading
注意:示例 AppleScript 代码就是这样,没有任何包含的错误处理 em> 不包含任何适当的附加错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statement 和error statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5,延迟的值设置得当。