![](/img/trans.png)
[英]Extracting href from a class within other h3/class classes with jsoup
[英]Extracting href from a class within other div/id classes with jsoup
您好,我正在嘗試從以下來源中提取“ title”類中的第一個href(該來源只是整個頁面的一部分,但是我正在使用整個頁面):
div id="atfResults" class="list results ">
<div id="result_0" class="result firstRow product" name="0006754023">
<div id="srNum_0" class="number">1.</div>
<div class="image">
<a href="http://www.amazon.co.uk/Essential-Modern-Classics-J-Tolkien/dp/0006754023/ref=sr_1_1?ie=UTF8&qid=1316504574&sr=8-1">
<img src="http://ecx.images-amazon.com/images/I/31ZcWU6HN4L._AA115_.jpg" class="productImage" alt="Product Details">
</a>
</div>
<div class="data">
<div class="title">
<a class="title titleHover" href="http://www.amazon.co.uk/Essential-Modern-Classics-J-Tolkien/dp/0006754023/ref=sr_1_1?ie=UTF8&qid=1316504574&sr=8-1">Essential Modern Classics - The Hobbit</a>
<span class="ptBrand">by J. R. R. Tolkien</span>
<span class="bindingAndRelease">(<span class="binding">Paperback</span> - 2 Apr 2009)</span>
</div>
我已經嘗試了選擇函數和getElementByClass的幾種變體,但都給了我一個“空”值,例如:
Document firstSearchPage = Jsoup.connect(fullST).get();
Element link = firstSearchPage.select("div.title").first();
如果有人可以幫助我解決該問題並推薦一些閱讀領域,以便將來我能避免該問題,將不勝感激。
CSS選擇器div.title
返回一個<div class="title">
,而不是您認為的鏈接。 如果要<a class="title">
,則應使用a.title
選擇器。
Element link = document.select("a.title").first();
String href = link.absUrl("href");
// ...
或者,如果在此之前<a class="title">
可以在<div class="title">
之外的文檔中的其他位置出現,則您需要以下更具體的選擇器:
Element link = document.select("div.title a.title").first();
String href = link.absUrl("href");
// ...
這將返回第一個<a class="title">
,它是<div class="title">
的子<div class="title">
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.