HTML 代码:
<td>
<img src="../images/items/333.png"><br>
<b>Product One</b><br>
(2 in stock)<br>
<i>65 USD</i><br>
<form action="shopping.php?shop=1&item=333&price=65&buy=conf&type=" method="post">
<input name="" value="Buy this item" type="submit">
</form></td>
<td>
<img src="../images/items/444.png"><br>
<b>Product Two</b><br>
(4 in stock)<br>
<i>5 USD</i><br>
<form action="shopping.php?shop=1&item=444&price=5&buy=conf&type=" method="post">
<input name="" value="Buy" type="submit">
</form></td>
这是我正在处理的页面上的 html 代码,无法更改 html 代码。
页面上有几个 td 标记,其中包含您在上面的代码中看到的以下信息。
我想写一个脚本来做这样的事情:
if (document.body.innerHTML.indexOf("Product One") > -1) {
document.location = "shopping.php?shop=1&item="+itemID+"&price="+USD+"&buy=conf&type="
}
在页面的 body/td 中搜索我的脚本中指定的“产品名称”,如果找到,则转到包含需要提取的变量 itemID 和 USD 的 url。
itemID通过取数字从 image.png 的 src 中提取。例如,itemID../images/items/444.png
为 444。
美元是从斜体标签之间定义的价格中提取的。例如,提取的美元值为<i>5 USD</i>
5。
捕获是
我需要很多来if (document.body.innerHTML.indexOf("Name") > -1) {document.location = "shopping.php?shop=1&item="+itemID+"&price="+USD+"&buy=conf&type="}
迎合我指定的大量产品。我可能想指定“产品一到一百”和“子产品 A 到 Z”
解决方案
我想到的一些处理方法(需要放入javascript代码)是:
- 将我将指定的产品列表放入一个数组(类似的东西)
var list = new Array ("Product One","Product Two","Sub-Product A");
,并让一个函数检查页面是否存在来自该数组的任何产品,该数组显示在页面上。 - 找到商品后,要获取itemID,将商品的图片src中
.png
前后的数字分离出来。/items/
而要获取美元,则获取<i> </i>
标签之间的值,并且只取数值 - 为此,我认为
nextSibling
或previousSibing
可以使用,但我对此不太确定。 - 或者,为了更容易,可以有一个函数来立即定位表单的操作值并设置
window.location
因为<form action="shopping.php?shop=1&item=444&price=5&buy=conf&type=" method="post">
- 我在使用 XPath 之前已经看到过这个?