I'm using jsoup to find classes from a webpage like this
Document doc = null;
try {
doc = Jsoup.connect(strings[0]).get();
// Get document (HTML page) title
String title = doc.title();
// Get meta info
Elements metaElems = doc.select("div");
for (Element metaElem : metaElems) {
if (metaElem.hasClass("job-title")){
System.out.println("found a job " + "\r\n" + metaElem.toString() ); //this works finds all job titles and links
}
if (metaElem.hasClass("detail-body")){
System.out.println("detail-body " + "\r\n" + metaElem.toString() ); //this works finds all job titles and links
}
}
} catch (IOException e) {
e.printStackTrace();
}
now i want to get child classes in the detail-body (see snippet below) like this
<li class="location">
trying to get it from my loop like
if (metaElem.hasClass("location")){
//do stuff
);
doesn't work and i think it's because its a child of detail-body class but im fairly new to this so could be wrong below is a snippet (one iteration) of what is printed from the above code anyone tell me how to get the info inside without playing with substrings (not that im against it i would just rather have everything well organised, i can use substrings if thats the only way)
found a job
<div class="job-title">
<a href="/job/class-2-driver/driveforce-job77343151" data-dynamic-qs="?
entryurl=%2fjobs%2fin-%3fradius%3d5%2377343151" title="See details for a
Class 2
Driver in West Midlands (matches on class 2 driver)"> <h2>Class 2
Driver</h2>
</a>
</div>
detail-body
<div class="detail-body">
<div class="row">
<div id="headerListContainer" class="col-xs-12 col-sm-8">
<div class="applied-col pull-right" style="display: none;">
<span class="applied-icon">Applied</span>
</div>
<ul class="header-list">
<li class="location"> <span> <span> <a href="/jobs/in-west-midlands">West
Midlands</a>, <span>WR1 1UK</span> </span> </span> </li>
<li class="salary" title="salary">Salary ranges from £9-£10
pounds per hour</li>
</ul>
</div>
<div id="recruiterImageContainer" class="col-xs-5 col-sm-4 pull-right">
<div class="recruiter-image">
<a href="/jobs-at/driveforce/jobs" title="DriveForce"> <img data-
original="/companylogos/0b01e3bd83ec4919a44b7b145725e15a.png"
class="lazy" /> </a>
</div>
</div>
<div class="col-xs-7 col-sm-8">
<ul class="detail-list">
<li class="job-type"> <span title="employment type">Contract</span> </li>
<li class="company" title="hiring organization"> <h3> <a href="/jobs-
at/driveforce/jobs" title="DriveForce">DriveForce</a> </h3> </li>
<li class="date-posted" title="posted date"> <span> Today </span> </li>
</ul>
</div>
</div>
<div class="row detail-footer">
<div class="col-sm-12 col-md-10">
<div title="job details">
<p class="job-intro">DriveForce are currently recruiting for Class 2
drivers for a post based in Kidderminster! Driver duties will involve
going to
various locations disposing of confidential documents. To be
considered drivers
must have held their Class 2 license for 2 years or longer, and have
no more
than 6 points on their licens...</p>
</div>
</div>
<div class="email-job-col visible-xs visible-sm col-xs-4 col-md-2 col-sm-
4">
<button type="button" class="btn btn-default btn-sendjob" data-job-
id="77343151" data-job-
token="tqhJrOYD5cVRoPcna3gQfN/0cu8XVm1rV/LjjT2lvIz+o7dcujmniqJQMk8Kix2L"
data-
toggle="modal" data-target="#sendJobModal">Send</button>
</div>
<div class="see-job-col visible-xs visible-sm col-xs-4 col-sm-4 col-md-4 ">
<a class="btn btn-default btn-seejob" href="/job/class-2-driver/driveforce-
job77343151" data-dynamic-qs="?entryurl=%2fjobs%2fin-
%3fradius%3d5%2377343151">See</a>
</div>
<div class="save-job-col col-xs-4 col-sm-4 col-md-2">
<button id="77343151" class="saved-jobs-icon btn btn-default btn-savejob
btn-mobile-hover-fix disabled" disabled="disabled">Save</button>
</div>
</div>
<div class="row hidden-xs">
<div class="col-xs-12">
<div class="discipline-related-links">
<ul>
<li class="col-xs-12 col-md-6"><a href="/jobs/logistics/in-west-
midlands">See more Logistics jobs in West Midlands</a></li>
<li class="col-xs-12 col-md-6"><a href="/jobs/logistics">See all
Logistics jobs</a></li>
</ul>
</div>
</div>
</div>
</div>
JSoup's selector syntax allows you to select all elements of a particular type having a given class.
In your question you stated
i want to get child classes in the detail-body (see snippet below) like this
<li class="location">
The following selector will find all elements of type li
having the class location
regardless of whether these elements are nested or not.
Document doc = Jsoup.parse(html);
Elements elements = doc.select("li.location");
for (int i = 0; i < elements.size(); i++) {
System.out.println(elements.get(i).text());
}
In the HTML attached to your OP the div with class=location
is nested inside a ul
with class=header-list
, you can use JSoup'sparent-child awareness to select only elements of type li
having the class location
which are nested inside <ul class="header-list">
, for example:
Elements elements = doc.select("ul.header-list > li.location");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.