Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element by class in JSoup

Tags:

java

jsoup

I try to get all info contained in div class named : bg_block_info, but instead i get info for another div class <div class="bg_block_info pad_20"> Why i'm getting it wrong ?

Document doc = Jsoup.connect("http://www.maib.md").get(); 
Elements myin = doc.getElementsByClass("bg_block_info");
like image 785
develoops Avatar asked Feb 17 '12 20:02

develoops


3 Answers

You can combine and chain selectors to refine your query, e.g.:

Document doc = Jsoup.connect("http://www.maib.md/").get(); Elements els = doc.getElementsByClass("bg_block_info").not(".pad_10").not(".pad_20"); 
like image 167
Hauke Ingmar Schmidt Avatar answered Oct 31 '22 13:10

Hauke Ingmar Schmidt


That element has two classes (notice the space between bg_block_info and pad_20):

<div class="bg_block_info pad_20">

So it does have the class bg_block_info and your code is working as expected.

like image 38
Wayne Avatar answered Oct 31 '22 11:10

Wayne


Elements downloadLinks = dContent.select("a[href]");
Elements pdfLinks = downloadLinks.select("a[data-format$=pdf]");

Full reference jsoup selector syntax

like image 20
Vladimir Stazhilov Avatar answered Oct 31 '22 12:10

Vladimir Stazhilov