(请注意,在本例中,我使用的是不同的产品,因为在我 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不再是正确的结果,或开始在任意位置添加更多价格时,它们都会失败.这是否是一个问题将取决于这种情况发生的频率,一旦您开始定期使用脚本,您就会发现这一点.
这可能不值得提前担心,除非这是一个关键的应用程序.一旦您看到页面随时间变化的趋势,您可能会发现上述解决方案中的一个比其他解决方案更好,或者还有另一个解决方案会更合适.