Hello I am trying to extract the first href from within the "title" class from the following source (the source is only part of the whole page however I am using the entire page):
您好我正在尝试从以下源中提取“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://rads.*.com/amzn/click/0006754023">
<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://rads.*.com/amzn/click/0006754023">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>
I have tried several variations of both the select function and also getElementByClass but all have given me a "null" value such as:
我已经尝试了select函数和getElementByClass的几种变体,但都给了我一个“null”值,例如:
Document firstSearchPage = Jsoup.connect(fullST).get();
Element link = firstSearchPage.select("div.title").first();
If someone could help me with a solution to this problem and recommend some areas of reading so I can avoid this problem in future it would be greatly appreciated.
如果有人可以帮我解决这个问题,并建议一些阅读领域,以便将来可以避免这个问题,我将不胜感激。
1 个解决方案
#1
0
The CSS selector div.title
, returns a <div class="title">
, not a link as you seem to think. If you want an <a class="title">
then you should use the a.title
selector.
CSS选择器div.title返回一个
Element link = document.select("a.title").first();
String href = link.absUrl("href");
// ...
Or if an <a class="title">
can appear elsewhere in the document outside a <div class="title">
before that point, then you need the following more specific selector:
或者,如果可以在该点之前出现在
Element link = document.select("div.title a.title").first();
String href = link.absUrl("href");
// ...
This will return the first <a class="title">
which is a child of <div class="title">
.
这将返回第一个,它是
#1
0
The CSS selector div.title
, returns a <div class="title">
, not a link as you seem to think. If you want an <a class="title">
then you should use the a.title
selector.
CSS选择器div.title返回一个
Element link = document.select("a.title").first();
String href = link.absUrl("href");
// ...
Or if an <a class="title">
can appear elsewhere in the document outside a <div class="title">
before that point, then you need the following more specific selector:
或者,如果可以在该点之前出现在
Element link = document.select("div.title a.title").first();
String href = link.absUrl("href");
// ...
This will return the first <a class="title">
which is a child of <div class="title">
.
这将返回第一个,它是