所以我可以使用 Applescript 来获取页面的标题:
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
.
我可以使用Applescript(或Javascript)以某种方式获取这个值吗? (可以是大概价格,不需要很精确。所以
£22
或22
都可以)
当我查看该页面的源代码时,我发现:
<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>
我如何使用 AppleScript 或 Javascript 为我获取
£22.86
?
我尝试过使用
document.getElementsByClassName('a-price a-text-price a-size-medium apexPriceToPay'[1].innerHTML;
但这不起作用。请问这是在正确的轨道上吗?
(请注意,在这个例子中,我使用的是不同的产品,因为在我写这篇文章时,Agua Brava 显然不再可用。)
你的例子是在正确的轨道上。但是,JavaScript 使用从零开始的索引,而不是从一开始的索引。
我验证这会产生“缺失值”:
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
,它满足您问题中可能期望的结果。但是,如果您想对其进行数学计算,则需要删除 £
。通过删除第一个字符最容易完成此操作。
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
,因为“£19.35”是第一个以“£”开头的文本,紧接着是 1-9 的数字,紧接着是 0-9,紧接着是句点,其后紧跟两个数字 0-9。因为这是一个正则表达式,所以您可以非常灵活地选择要搜索的内容和要避免的内容。
正则表达式
match
使用索引1而不是索引0,因为索引0是完整匹配,包括价格后面的任何非数字字符;索引 1 是第一个(在本例中也是唯一一个)括号匹配。
所有这些方法都存在一个问题,即当亚马逊更改类名称,或更改页面布局以使索引零不再是正确结果,或开始在任意位置添加更多价格时,它们都会失败。这是否是一个问题取决于这种情况发生的频率,一旦您开始定期使用脚本,您就会发现这一点。
除非这是一个关键的应用程序,否则可能不值得提前担心。一旦您了解页面随时间变化的趋势,您可能会发现上述解决方案之一比其他解决方案更好,或者另一种解决方案更合适。