【问题标题】:Get data from webpage via Apple script通过 Apple 脚本从网页获取数据
【发布时间】:2021-04-28 08:45:27
【问题描述】:

我正在尝试从网页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

【问题讨论】:

  • 感谢您迄今为止所拥有的一切。你可以在每一行中使用percentage的类名吗?

标签: applescript


【解决方案1】:

这里有另一种方式供您考虑:

请注意,这需要在 Safari 中隐藏的开发菜单上选中Allow JavaScript from Apple Events

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

  • 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 代码 就是这样,没有任何包含的错误处理不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

【讨论】:

  • 您好。谢谢你在这里回答。选择“允许来自 Apple 事件的 Javascript”是否存在安全问题?
猜你喜欢
  • 2019-05-30
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多