methuselah
methuselah

Reputation: 13206

Jsoup get element inside element

I am trying to scrape the following page: https://icobench.com/icos and I am stuck trying to extract bits of information from elements with the ico_data class. The code looks like this:

<td class="ico_data">
  <div class="image_box"><a href="/ico/gcox" class="image" style="background-image:url('/images/icos/icons/gcox.jpg');"></a></div>
  <div class="content">
    <a class="name" href="/ico/gcox"><object><a href="/premium" title="Premium" class="premium">&nbsp;</a></object>GCOX</a>
    <p>GCOX is the world's first blockchain-powered platform that allows the popularity of celebrities to be tokenised and listed.<br><br><b>Restrictions KYC:</b> Yes <span class="line">|</span> <b>Whitelist:</b> Yes <span class="line">|</span> <b>Countries:</b>      USA, Singapore</p>
  </div>
  <div class="shw">
    <div class="row"><b>Start:</b> 08 Aug 2018</div>
    <div class="row"><b>End:</b> 31 Aug 2018</div>
    <div class="row"><b>Rate:</b>
      <div class="rate color4">3.9</div>
    </div>
  </div>
</td>

I'd like to extract the name, description, start date, end date. How do I go about it?

This is my code so far:

Document document = Jsoup.connect("https://icobench.com/icos").userAgent("Mozilla").get();    
Elements companyElements = document.getElementsByClass("ico_data");
for (Element companyElement : companyElements) {
   // do stuff here
}

Thanks,

Upvotes: 1

Views: 1382

Answers (1)

Prathibha Chiranthana
Prathibha Chiranthana

Reputation: 822

You can Filter out the Start and End By filtering Tags with contains. Name by the class "name" and the description by the P tag inside content div.

public void extract(){
        try {
            Connection connection = Jsoup.connect("https://icobench.com/icos");
            Document document = connection.get();
            Elements companyElements = document.select(".ico_data");
            for (Element companyElement : companyElements) {

                if(companyElement.select(".content")!=null&&companyElement.select(".content").size()>0){

                    Element content = companyElement.select(".content").first();
                    String name = companyElement.select(".content").select(".name").text();
                    String description = companyElement.select(".content").select("p").text();
                    String start = companyElement.select("b:contains(Start)").first()
                            .parent().text().replace(companyElement.select("b:contains(Start)").first().text(),"");
                    String end = companyElement.select("b:contains(End)").first()
                            .parent().text().replace(companyElement.select("b:contains(End)").first().text(),"");

                }

                System.out.println(companyElement);
                // do stuff here
                }
        } catch (IOException e) {
            e.printStackTrace();
        }

Upvotes: 2

Related Questions