我的AppleScript代码出了什么问题?从搜索中提取URL

时间:2017-10-14 23:29:38

标签: html applescript applescript-excel

我有许多UPC的列表,我需要在亚马逊上搜索UPC,然后返回弹出的第一个产品的URL。我正在尝试编写AppleScript来快速完成此操作,我可以根据我必须搜索的UPC数量来更改迭代次数。

UPC的所有搜索链接都在A列的Excel文件中(例如https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dtoys-and-games&field-keywords=889698110563)。 B列为空白,用于显示结果的预期链接。

以下是我写的内容,但它不起作用。请帮我排除故障!

repeat 5 times
    activate application "Microsoft Excel"
    tell application "System Events" to tell process "Microsoft Excel"
        keystroke "c" using command down
        key code 124
    end tell

    tell application "System Events" to tell process "Google Chrome"
        keystroke "l" using command down
        keystroke "v" using command down
        key code 36
    end tell

    tell application "System Events" to tell process "Google Chrome"
        set frontmost to true
        repeat 39 times
            keystroke tab
        end repeat
        key code 36
    end tell

    tell application "Google Chrome"
        set the clipboard to (URL of active tab of first window as text)
    end tell

    tell application "System Events" to tell process "Microsoft Excel"
        keystroke "v" using command down
        key code 125
        key code 123
    end tell
end repeat

1 个答案:

答案 0 :(得分:0)

我没有或使用 Excel ,所以我对编码直接使用没有帮助,但这里有示例 AppleScrip t 脚本我如何编码以实现 UPC搜索网址和相应网址 UPC搜索网址返回的第一项<>>,然后在导入/打开的文件中,例如 Excel 或类似的应用。

从仅包含 UPC搜索网址纯文本文件开始,即 A列的内容,例如:

https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dtoys-and-games&field-keywords=889698110563
https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dtoys-and-games&field-keywords=889698110532
https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dtoys-and-games&field-keywords=745559220041
https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dtoys-and-games&field-keywords=849803049096
https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dtoys-and-games&field-keywords=745559239104

脚本编辑器中...我将此文件读入变量,创建 AppleScript 列表从中,每次搜索网址一次在 Google Chrome 中打开一个。然后使用 JavaScript ,与标签共39次,检索每个 UPC搜索网址返回网址 ,反过来又放入另一个 AppleScript 列表。然后,我将 AppleScript 列表连接到标签分隔的文件中,然后可以导入/打开app已注册处理具有 .csv 扩展名的文件。

从那里,您可以将其另存为 .xls / .xlsx 文件,或将信息复制并粘贴到原始 Excel 文件或其他任何内容。

  • 注意:要在 Excel 中正确打开 .csv 文件,必须将其导入(在MSO 2016中,文件&gt;导入)。只需双击它,它将打开,它将不会解析选项卡分隔符。

示例AppleScript 代码

--  # Input File

set UPC_File to POSIX path of (path to documents folder as text) & "UPC Search List.txt"

--  # Output File

set UPC_URL_File to POSIX path of (path to documents folder as text) & "UPC URL List.csv"

--  # Use a couple AppleScript Lists to hold information for processing.

set UPC_Search_List to {}
set URL_Returned_List to {}

--  # Read contents of UPC_File into a variable.

set UPC_File_Content to do shell script "cat " & quoted form of UPC_File

--  # Load UPC_Search_List from UPC_File_Content variable.

repeat with UPC_Search_URL in paragraphs of text of UPC_File_Content
    set end of UPC_Search_List to UPC_Search_URL as text
end repeat

--  # Open a new Google Chrome window to 'about:blank'.
--  # This is used as a buffer to keep the window from 
--  # being closed as the ensuing active tab is closed.

tell application "Google Chrome"
    make new window
    activate
    set URL of active tab of front window to "about:blank"
    delay 1
    --  # Load corresponding URL_Returned_List for each item in UPC_Search_List.
    repeat with thisURL in UPC_Search_List
        open location thisURL
        --  # Wait for Tab to finish loading.
        repeat until (loading of active tab of window 1 is false)
            delay 0.5
        end repeat
        --  # Get URL of first item returned of UPC Search.
        try
            tell active tab of front window
                set end of URL_Returned_List to execute javascript "document.getElementsByClassName('a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal')[0].href;"
            end tell
        on error
            set end of URL_Returned_List to missing value
        end try
        delay 3 -- # A delay is necessary so as not to hammer the website with requests, which can/may lead to blacklisting your IP Address.
        close active tab of front window
    end repeat
    close front window
end tell

--  # Concatenate UPC_Search_List and URL_Returned_List into UPC_URL_File creating a tab delimited file.

repeat with i from 1 to count of UPC_Search_List
    do shell script "echo " & quoted form of item i of UPC_Search_List & quoted form of tab & quoted form of item i of URL_Returned_List & " >> " & quoted form of UPC_URL_File
end repeat

--  # Inform user script finished.

activate
display dialog "Processing of the UPC Search List has finished." buttons {"OK"} default button 1

您现在可以输出文件 导入 Excel

以下是直接使用 Excel 示例 AppleScript 脚本的版本:

  • 它仍使用两个 AppleScript 列表,首先在列中加载单元格的 列表之一指定的数量,然后获取第一项的相应网址 Google Chrome 中返回,将其加载到第二个列表中。然后, B列中的单元格将从第二个列表中设置。

示例AppleScript 代码

--  # Use a couple AppleScript Lists to hold information for processing.

set UPC_Search_List to {}
set URL_Returned_List to {}

--  # Load UPC_Search_List from Excel.

tell application "Microsoft Excel"
    tell sheet 1 of window 1
        repeat with i from 1 to 5
            set end of UPC_Search_List to value of cell ("A" & i)
        end repeat
    end tell
end tell

--  # Open a new Google Chrome window to 'about:blank'.
--  # This is used as a buffer to keep the window from 
--  # being closed as the ensuing active tab is closed.

tell application "Google Chrome"
    make new window
    activate
    set URL of active tab of front window to "about:blank"
    delay 1
    --  # Load corresponding URL_Returned_List for each item in UPC_Search_List.
    repeat with thisURL in UPC_Search_List
        open location thisURL
        --  # Wait for Tab to finish loading.
        repeat until (loading of active tab of window 1 is false)
            delay 0.5
        end repeat
        --  # Get URL of first item returned of UPC Search.
        try
            tell active tab of front window
                set end of URL_Returned_List to execute javascript "document.getElementsByClassName('a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal')[0].href;"
            end tell
        on error
            set end of URL_Returned_List to missing value
        end try
        delay 3 -- # A delay is necessary so as not to hammer the website with requests, which can/may lead to blacklisting your IP Address.
        close active tab of front window
    end repeat
    close front window
end tell

--  # Write URL_Returned_List to Excel.

tell application "Microsoft Excel"
    tell sheet 1 of window 1
        repeat with i from 1 to 5
            set value of cell ("B" & i) to item i of URL_Returned_List
        end repeat
    end tell
end tell

--  # Inform user script finished.

activate
display dialog "Processing of the UPC Search List has finished." buttons {"OK"} default button 1

注意:这些只是示例 AppleScript 脚本,并且可以采用不同的编码方式。除了一个tryon error 语句之外,它们不包含可能被视为适当,必要和/或需要的任何其他错误处理。用户有责任相应地实施错误处理