简体   繁体   English

如何使用jsoup从网页获取子类

[英]how to use jsoup to get child classes from a webpage

I'm using jsoup to find classes from a webpage like this 我正在使用jsoup从这样的网页中查找类

        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 现在我想像这样在detail-body中获得子类(请参见下面的代码段)

<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) 不起作用,我认为这是因为它是detail-body类的子级,但是对此我还很陌生,因此下面的内容可能是错误的(上面的代码是其中一个片段的重复),有人告诉我如何获得内部信息而不使用子字符串(不是即时消息,我宁愿一切都井井有条,如果那是唯一的方法,我可以使用子字符串)

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 &pound;9-&pound;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. JSoup的选择器语法允许您选择具有给定类的特定类型的所有元素。

In your question you stated 在你的问题中你说

i want to get child classes in the detail-body (see snippet below) like this <li class="location"> 我想像这样<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. 以下选择器将查找具有类location li类型的所有元素,而不管这些元素是否嵌套。

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: 在附加到OP与DIV的HTML class=location嵌套在内部ulclass=header-list ,则可以使用JSoup'sparent胎意识选择类型的唯一的元件li具有类location被嵌套在<ul class="header-list"> ,例如:

Elements elements = doc.select("ul.header-list > li.location");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM