通过 Apple 脚本从网页获取数据

时间:2021-04-27 09:27:24

标签: applescript

我正在尝试从网页 https://www.worldcoinindex.com/

中获取加密名称、价格和 % 的数据

如何通过以下脚本获取 % 列值?

set theHtml to do shell script "curl -s " & quoted form of "https://www.worldcoinindex.com"
set text item delimiters to {"<tbody>", "</tbody>"}
set tableContents to theHtml's text item 2 # item 2 is the body of the price table
set text item delimiters to {"<h2>"} # site uses new h2 for each currency
set tableChunks to tableContents's text items 2 thru -1
set pasteStr to ""
repeat with aChunk in tableChunks
    set text item delimiters to "><span>$ </span><span class=\"span\">"
    tell aChunk's text item 1 to set {theSymbol, thePrice} to {first word, last word}
    set pasteStr to pasteStr & theSymbol & tab & thePrice & return
end repeat
set the clipboard to pasteStr

1 个答案:

答案 0 :(得分:0)

这是供您考虑的替代方法:

请注意,这需要在 Safari 中隐藏的开发菜单上选中允许来自 Apple 事件的 JavaScript

要取消隐藏隐藏的开发菜单:

  • Safari > 偏好... > 高级 > [√] 在菜单栏中显示开发菜单

示例 AppleScript 代码

tell application "Safari"
    
    make new document ¬
        with properties {URL:"https://www.worldcoinindex.com"}
    
    my waitForSafariPageToFinishLoading()
    
    tell front document
        
        set tickerList to {}
        set lastPriceList to {}
        set percentageList to {}
        
        repeat with i from 0 to 99
            set ticker to ¬
                do JavaScript ¬
                    "document.getElementsByClassName('ticker')[" & i & "].innerText;"
            copy words of ticker as text to end of tickerList
            set lastPrice to ¬
                do JavaScript ¬
                    "document.getElementsByClassName('number pricekoers lastprice')[" & i & "].innerText;"
            copy lastPrice to end of lastPriceList
            
            set percentage to ¬
                do JavaScript ¬
                    "document.getElementsByClassName('percentage')[" & i & "].innerText;"
            copy percentage to end of percentageList
        end repeat
        
    end tell
    
end tell

set tempListItem to {}
set groupedItemsList to {}
repeat with i from 1 to 100
    copy item i of tickerList to end of tempListItem
    copy item i of lastPriceList to end of tempListItem
    copy item i of percentageList to end of tempListItem
    copy tempListItem to end of groupedItemsList
    set tempListItem to {}
end repeat

set tabDelimitatedListAsText to ""
repeat with anItem in groupedItemsList
    set {TID, AppleScript's text item delimiters} to ¬
        {AppleScript's text item delimiters, tab}
    set thisItem to text of anItem as text
    set AppleScript's text item delimiters to TID
    set tabDelimitatedListAsText to ¬
        tabDelimitatedListAsText & thisItem & linefeed
end repeat

set the clipboard to tabDelimitatedListAsText


on waitForSafariPageToFinishLoading()
    --  # Wait for page to finish loading in Safari.
    --  # This works in macOS Catalina and 
    --  # macOS Big Sur and may need adjusting
    --  # for other versions of macOS.
    
    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 代码就是这样,没有任何包含的错误处理不包含任何额外的错误处理。用户有责任根据需要添加任何错误处理。查看 try 中的 error statementAppleScript Language Guide statement。另见Working with Errors。此外,在适当的情况下,可能需要在事件之间使用 delay command,例如delay 0.5,适当设置 delayvalue