Reputation: 622
I am trying to extract a href from a div class within another div class. One example of a code snippet i am trying to use is:
<div class="productData">
<div class="productTitle">
<a href="https://rads.stackoverflow.com/amzn/click/com/0786866020" rel="nofollow noreferrer"> Fish! A Remarkable Way to Boost Morale and Improve Results</a>
<span class="ptBrand">by <a href="/Stephen-C.-Lundin/e/B001H6UE16">Stephen C. Lundin</a>, <a href="/Harry-Paul/e/B001H9XQJA">Harry Paul</a>, <a href="/John- Christensen/e/B003VKXJ04">John Christensen</a> and Ken Blanchard</span>
<span class="binding"> (<span class="format">Hardcover</span> - Mar. 8, 2000) </span>
</div>
I am trying to extract the innter class productTitle from this example however using the code:
Document doc = Jsoup.connect(fullST).timeout(10*1000).get();
Element title = doc.getElementById("div.productTitle");
System.out.println(title);
I get null. Trying to extract higher elements such as:
Element title = doc.getElementById("div.productData");
I also get null. I have tried many code combinations but cannot figure out the syntax to extract from inner div classes or inner ids.
Any help would be appreciated.
Upvotes: 0
Views: 5366
Reputation: 1109875
You're trying to select the element by ID using getElementById()
. This is wrong. Those div's don't have an ID. Instead, they have a classname. You should use the select()
method instead.
Element title = doc.select("div.productTitle").first();
Note that the classname selector doesn't necessarily return a single element. There can be multiple of them in the document. I assume that you need the first and the only Element
, so I added first()
call to the example.
Upvotes: 1