AppleScript从网页上读取内容

时间:2015-03-28 07:47:12

标签: search applescript webpage

我一直在努力学习AppleScript几个月来解决这个问题。

这是我对Stackoverflow的第一个问题,所以如果我发布了incorreclty,请告诉我。

我一直在研究这个网站和其他人,试图了解我需要让AppleScript做我想做的事情。 在这个学习过程中,我想单独使用AppleScript来完成这些任务。

将来我计划学习Python,JAVA,shell脚本等等。目前我专注于AppleScript。

我的目标是:

  • 打开网页。

  • 进行搜索。

  • 选择其中一个结果。

  • 在结果页面上搜索。

  • 复制相对于搜索字符串的一些文字。

  • 将其粘贴到文本文档中。

  • 从结果页面复制其他内容

  • 将其粘贴到同一文本文档中。

-- Write Document Text  
set DocText to ""

-- Look at TextEdit
tell application "TextEdit"
    activate
    make new document
    -- OPENING REMARKS 
    set DocText to DocText & "Below is the content copied from a web page." & linefeed as string
end tell
-- ^ That part works


-- Look at Safari   
tell application "Safari"
    activate
    -- Open URL
    tell application "System Events"
        open location "http://www.ss42.com/toys.html"
        -- Select Search box 
        delay 1
        keystroke tab
        -- Enter search string
        delay 3
        keystroke "cat"
        -- Search
        delay 3
        keystroke return
    end tell
end tell
-- ^ Results of search
-- ^ That section works correctly


-- Click Link on results page. ( I don't know how to do this. )
-- Search for text string in webpage ( I don't know how to do this. )
-- Copy ContentRelitive to text string search into variable name ( I don't know how to do this. )
set ContentRelitive to "Content from webpage relative to search string. "
-- Find AdditionalStuff from page as above
-- Copy more stuff from webpage into variable name as above
set AdditionalStuff to "Additional Stuff from web page. "
-- This ^ is what I NEED to learn!!!


-- Paste all content into DocText
tell application "TextEdit"
    set DocText to DocText & linefeed & "Blank Space " & ContentRelitive & "Blank Space " & AdditionalStuff & "Blank Space " as string
    set the text of the front document to DocText & "Space Filler. " as string
    -- ^ That part works properly
end tell

1 个答案:

答案 0 :(得分:1)

你离我不远。大多数代码都有效,你只需要知道Safari部分。

但我稍微调整了一下代码。

开放位置是标准添加命令的一部分,因此您无需将它们放在任何应用程序的tell块中。

如果不需要将代码放在tell块中,你应该总是尽量避免将代码放入。这将限制语法和命令冲突。

您应该在代码中查看您正在使用的每个程序的Applescript词典。

转到窗口 - >图库菜单,打开图书馆窗口。

这将有一个应用程序列表。双击一个将显示它的字典,它将尝试向您展示如何使用单个应用程序理解的命令和语法。您可以将其他应用程序拖到窗口,将其添加到库中。或使用它的+按钮。将添加任何具有字典的应用程序。那些不可编写脚本的人不会,你会得到一条消息说明。

阅读苹果简介AppleScriptLangGuide 并阅读Applescript Library中的 StandardAdditions 字典

-- Write Document Text  
set DocText to ""



-- Look at Safari   
tell application "Safari"
    activate

end tell
open location "http://www.ss42.com/toys.html"

delay 1
tell application "System Events"
    -- Open URL
    tell application process "Safari"

        -- Select Search box 

        keystroke tab
        -- Enter search string
        delay 3
        keystroke "cat"
        -- Search
        delay 3
        keystroke return
    end tell

end tell





tell application "Safari"
    set AdditionalStuff to text of document 1
    -- This ^ is what I NEED to learn!!!
end tell

-- Paste all content into DocText
tell application "TextEdit"
    activate

    set thisDoc to make new document

    -- OPENING REMARKS 
    set ContentRelitive to "Content from webpage relative to search string. " & return & return

    set DocText to "Below is the content copied from a web page." & return & return & ContentRelitive & return & space & AdditionalStuff
    tell thisDoc
        set it's text to DocText & "Space Filler. "
    end tell

end tell

您还可以通过直接打开和提交搜索页面来删除大量代码。

这是将结果页网址用作起始网址并插入搜索字词的常见技巧。

set searchWord to "dog"

open location "http://www.ss42.com/5481/search.php?zoom_query=" & searchWord & "&submit=Search"

delay 3

tell application "Safari"
    activate
    set AdditionalStuff to text of document 1
    -- This ^ is what I NEED to learn!!!
end tell

-- Paste all content into DocText
tell application "TextEdit"
    activate
    set thisDoc to make new document

    -- OPENING REMARKS 
    set ContentRelitive to "Content from webpage relative to search string. " & return & return

    set DocText to "Below is the content copied from a web page." & return & return & ContentRelitive & return & space & AdditionalStuff
    tell thisDoc
        set it's text to DocText & "Space Filler. "
    end tell
end tell

如果我没记错,使用开放位置将使用默认浏览器

因此,所有代码都假定 Safari 是默认浏览器