Overview

因此,我可以使用AppleScrip来获取页面标题,方法是:

tell application "Google Chrome" to return title of active tab of front window

例如,如果我打开了https://www.amazon.co.uk/Agua-Brava-Men-EDC-Splash/dp/B000E7YK0U/页,我可以看到当前的价格是£22.86.

screenshot of amazon.co.uk page of a random item with price highlighted

我可以使用AppleScrip(或Java脚本)以某种方式获取这个值吗?(它可以是大约的价格,它不需要精确.因此,£2222是可以接受的)

在Amazon.co.uk页面上查看源代码

当我查看该页面的源代码时,我发现:

<span class="a-price a-text-price a-size-medium apexPriceToPay" data-a-size="b" data-a-color="price"><span class="a-offscreen">£22.86</span><span aria-hidden="true">£22.86</span></span>

view source for the random amazon-uk item

我如何才能使用AppleScrip或Java脚本为我获得£22.86

推荐答案

(请注意,在本例中,我使用的是不同的产品,因为在我 compose 本文时,Agua Brava显然不再可用.)

你的榜样走上了正确的道路.但是,JavaScript使用从零开始的索引,而不是从1开始的索引.

我验证了这会产生一个"缺失值":

tell application "Google Chrome"
    tell window 1
        tell tab 1
            execute javascript "document.getElementsByClassName('a-price a-text-price a-size-medium apexPriceToPay')[1].innerHTML"
        end tell
    end tell
end tell

然后,我将1指数替换为0:

tell application "Google Chrome"
    tell window 1
        tell tab 1
            execute javascript "document.getElementsByClassName('a-price a-text-price a-size-medium apexPriceToPay')[0].innerHTML"
        end tell
    end tell
end tell

并返回这span个元素所包含的HTML代码:

<span class="a-offscreen">£19.35</span><span aria-hidden="true">£19.35</span>

要获得实际价格,您需要获得该跨度的某个子元素的innerHTML.类似于:

tell application "Google Chrome"
    tell window 1
        tell tab 1
            set productPrice to execute javascript "document.getElementsByClassName('a-price a-text-price a-size-medium apexPriceToPay')[0].children[0].innerHTML"
        end tell
    end tell
end tell
productPrice

这将产生£19.35,这将满足您问题中可能的预期结果.然而,如果你想在它上面做数学运算,你会想要go 掉£.要做到这一点,最容易的方法是删除第一个字符.

tell application "Google Chrome"
    tell window 1
        tell tab 1
            set productPrice to execute javascript "document.getElementsByClassName('a-price a-text-price a-size-medium apexPriceToPay')[0].children[0].innerHTML"
        end tell
    end tell
end tell
set productPrice to characters 2 thru (number of characters of productPrice) of productPrice as string

请注意,虽然这是将productPrice设置为字符串-这是将字符粘合在一起所必需的,因为characters x thru y of string会生成单个字符的列表-但AppleScript并不是强类型的.如果字符串可以很容易地转换为数字,则可以对其进行数学运算,就像字符串19.35(在本例中)一样.例如,如果需要对其进行四舍五入,可以使用:

tell application "Google Chrome"
    tell window 1
        tell tab 1
            set productPrice to execute javascript "document.getElementsByClassName('a-price a-text-price a-size-medium apexPriceToPay')[0].children[0].innerHTML"
        end tell
    end tell
end tell
set productPrice to characters 2 thru (number of characters of productPrice) of productPrice as string
set productPrice to round (productPrice)

这产生结果19,成功地对字符串19.35进行舍入.

还有其他方法可以让你读到文本£19.35.实际包含文本的span具有明显唯一的类a-offscreen,这使得更短的命令成为可能:

tell application "Google Chrome"
    tell window 1
        tell tab 1
            set productPrice to execute javascript "document.getElementsByClassName('a-offscreen')[0].innerHTML"
        end tell
    end tell
end tell

或者,它可能(也可能不会…)更可靠地获取最近的具有id的标记,在本例中显示为corePrice_feature_div,然后从该标记向下钻取:

tell application "Google Chrome"
    tell window 1
        tell tab 1
            set productPrice to execute javascript "document.getElementById('corePrice_feature_div').children[0].children[0].children[0].innerHTML"
        end tell
    end tell
end tell

您甚至可以避免通过搜索标记来获取文本,而只是在整个页面上运行正则表达式:

tell application "Google Chrome"
    tell window 1
        tell tab 1
            set productPrice to execute javascript "document.body.innerHTML.match(/(£[1-9][0-9]\\.[0-9][0-9])[^0-9]/)[1]"
        end tell
    end tell
end tell

这也返回£19.35,因为"GB 19.35"是第一个文本,它以"GB"开头,后面紧跟1-9之间的数字,然后紧跟0-9,紧接着是句点,紧跟两个数字0-9.因为这是一个正则表达式,所以在如何 Select 要搜索和要避免的内容方面有很大的灵活性.

正则表达式match使用索引1而不是索引0,因为索引0是full匹配,包括价格后面的任何非数字字符;索引1是第一个(在本例中是唯一的)括号匹配.

所有这些方法都有一个问题,即当Amazon更改类名,或更改页面布局,使索引0不再是正确的结果,或开始在任意位置添加更多价格时,它们都会失败.这是否是一个问题将取决于这种情况发生的频率,一旦您开始定期使用脚本,您就会发现这一点.

这可能不值得提前担心,除非这是一个关键的应用程序.一旦您看到页面随时间变化的趋势,您可能会发现上述解决方案中的一个比其他解决方案更好,或者还有另一个解决方案会更合适.

Javascript相关问答推荐

订阅操作顺序

为什么子组件没有在reaction中渲染?

除了在Angular 16中使用快照之外,什么是可行且更灵活的替代方案?

积分计算和 colored颜色 判断错误

Next.js Next/Image图像隐含性有任何类型-如何修复?

根据总价格对航班优惠数组进行排序并检索前五个结果- Angular HTTP请求

使用axios.获取实时服务器时的404响应

单击子元素时关闭父元素(JS)

GrapeJS -如何保存和加载自定义页面

使用useEffect,axios和useParams进行react测试

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

将本机导航路由react 到导航栏中未列出的屏幕?

无法从NextJS组件传递函数作为参数'

在react JS中映射数组对象的嵌套数据

在JS中拖放:检测文件

我怎么在JS里连续加2个骰子的和呢?

向数组中的对象添加键而不改变原始变量

与svg相反;S getPointAtLength(D)-我想要getLengthAtPoint(x,y)

未捕获的运行时错误:调度程序为空

将Singleton实例设置为未定义后的Angular 变量引用持久性行为