我正在为JSoup (再一次)第一次道歉而苦苦挣扎,因为我反复询问,但学习得还不够好-所以有没有人能建议我在通常的搜索之外阅读一些东西,帮助我理解如何在每次尝试的时候破译DOM?
如果您仍然愿意提供帮助,这一次,在返回的文档中,我有:
<a href="/logitech-logitech-wireless-keyboard-k270-with-long-range-wireless-920-003051/info"><span id="priceProductQA1" class="productPrice">$29.99</span></a>我想要得到href和价格"29.99“。我试过了
doc = Jsoup.connect(srchStr).get();
for (Element choices : doc.select("a:has(.productPrice)")){
absHref = choices.attr("abs:href");
String pricetxt = choices.text();还有大约10种其他的方法都没有用。有没有更好的主意给我?
发布于 2013-01-29 00:12:59
这里有另一个解决方案:
for( Element element : doc.select("span.productPrice") ) // Select all 'span' tags with a 'productPrice' class and iterate over them
{
final String price = element.text(); // save the price (= text of element)
final String link = element.parent().absUrl("href"); // Get the 'a' tag (= parent of 'span' tag) and its absolute (!) URL
// ...
}说明:
span标记,因为您可以很容易地确定它是否是您需要的(有类,而a没有)span标记的父级URL的价格,因为它包含所需的URL<代码>H214<代码>H115选择绝对element顺便说一句。如果你是100%舒尔,在这个网站上只有一个这样的元素,你可以用Element element = doc.select("span.productPrice").first();替换for-Loop,后面跟着另外两行代码。
https://stackoverflow.com/questions/14563216
复制相似问题