0

它真的很简单。这是我的代码中正在执行该操作的部分。问题是我试图从网站上获取的信息没有输入到 Excel 文档中。

<div class="book-price">
                    $53.25                </div> == $0

这就是网站上的代码的样子。我想要的只是输入的 53.25 美元。

set i to 2

repeat 725 times

    activate application "Microsoft Excel"

    tell application "Microsoft Excel"

    end tell

    tell application "System Events"
        keystroke "c" using command down
        delay 1
    end tell

    activate application "Google Chrome"

    tell application "System Events"
        keystroke "t" using command down
        delay 1
        keystroke "https://bookscouter.com/prices.php?isbn="
        keystroke "v" using command down
        keystroke "&searchbutton=Sell"
        delay 1
        keystroke return
        delay 10
    end tell

    set theText to getInputByClass("book-prices", 0)
    tell application "Microsoft Excel"
        set value of cell ("k" & (i as string)) to theText
    end tell

    set i to i + 1

    tell application "System Events"
        keystroke "w" using command down
    end tell

end repeat

to getInputByClass(theClass, num)

    tell application "Google Chrome"
        tell active tab of window 1
            set input to execute javascript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;"
        end tell
    end tell

    return input

我认为这是我没有看到的非常基本的东西。最初粘贴的信息是isbns。例如 9781464139055。

谢谢!

4

1 回答 1

0

这是一个简单的示例,因为每个循环的值都相同,但它正在工作。它基本上是您正在使用的代码(唯一的区别是循环和输入文本的实现)。

on run
    set theText to "Sample Input"
    repeat with i from 2 to 300
        tell application "Microsoft Excel"
            set value of cell ("k" & (i as string)) to theText
        end tell
    end repeat
end run

我唯一能猜到的是您的输入文本不是您所期望的。在查看您的代码时,您似乎试图创建一个您自己的函数,称为getInputByClass? 不确定这是否只是您帖子上的拼写错误,但该功能的格式不正确,这可能是您的问题。

这是您的代码的轻微修改......

on run
    repeat with i from 2 to 725

        tell application "Microsoft Excel" to activate -- these activate lines may not be necessary 

        tell application "System Events"
            keystroke "c" using command down
            delay 1
        end tell

        tell application "Google Chrome" to activate -- these activate lines may not be necessary 

        tell application "System Events"
            keystroke "t" using command down
            delay 1
            keystroke "https://bookscouter.com/prices.php?isbn="
            keystroke "v" using command down
            keystroke "&searchbutton=Sell"
            delay 1
            keystroke return
            delay 10
        end tell

        set theText to getInputByClass("book-prices", 0)
        tell application "Microsoft Excel"
            set value of cell ("k" & (i as string)) to theText
        end tell

        tell application "System Events"
            keystroke "w" using command down
        end tell

    end repeat
end run


on getInputByClass(theClass, num)
    try
        tell application "Google Chrome"
            tell active tab of window 1
                set input to execute javascript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;"
            end tell
        end tell

        return input
    on error
        return "Error getting input"
    end try
end getInputByClass

另外一点,我认为您可以对这个脚本进行其他改进。您不必使用keystrokes从 Excel 获取内容并让 Chrome 打开新选项卡并导航到页面。这可以通过编写 Excel 和 Chrome 脚本直接完成。如果您可以摆脱使用System Eventskeystroke您的脚本将更加稳定。

例如,您可以使用它,而不是尝试从您在 Excel 中选择的单元格中复制内容。

tell application "Microsoft Excel" to set theText to string value of selection

然后您可以使用它来创建您的最终 URL,如下所示...

set MyURL to  "https://bookscouter.com/prices.php?isbn=" & theText & "&searchbutton=Sell"

更新 !!!

我有更多时间来运行您的代码,而您的问题是您的 javascript 没有在页面上找到该类,它不存在。

我认为您还有一些事情要处理您的代码,因为您似乎要继续打开和关闭相同的页面,这显然是浪费精力。此外,您似乎在以获取内容的方式不断为您的 Excel 工作表定位相同的数据,而不是在函数调用中增加数字。无论如何,下面的更新代码与您的代码相似,但已清理并执行我认为您正在尝试执行的操作。我已将 URL 的开头移到重复循环之外,这将显着提高您正在执行的操作的速度。

对代码进行更深入的返工

on run
    tell application "Microsoft Excel" to set theText to string value of selection
    set MyURL to "https://bookscouter.com/prices.php?isbn=" & theText & "&searchbutton=Sell"
    tell application "Google Chrome" to open location MyURL
    do shell script "sleep 1" -- give the tab time to load
    tell application "Google Chrome" to set newTab to active tab of window 1
    do shell script "sleep 3" -- give the page time to load

    repeat with i from 2 to 725
        set theText to getInputByClass("book-price-normal", i - 2)
        if theText as text is not "Missing Value" then
            tell application "Microsoft Excel" to set value of cell ("k" & (i as string)) to theText
        else
            exit repeat
        end if
    end repeat
    tell application "Google Chrome" to close newTab
end run


on getInputByClass(theClass, num)
    try
        tell application "Google Chrome"
            tell active tab of window 1
                set input to execute javascript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;"
            end tell
        end tell

        return input
    on error
        return "Error getting input"
    end try
end getInputByClass
于 2016-07-19T19:37:00.840 回答