Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing a table with jsoup

I'm trying to extract the e-mail adress and the phone number from a linkedin profile using jsoup, each of these informations is in a table. I have written a code to extract them but it doesn't work, the code should work on any linkedin profile. Any help or guidance would be much appreciated.

public static void main(String[] args) {
    try {

        String url = "https://fr.linkedin.com/";
        // fetch the document over HTTP
        Document doc = Jsoup.connect(url).get();

        // get the page title

        String title = doc.title();
        System.out.println("Nom & Prénom: " + title);
        //  first method
        Elements table = doc.select("div[class=more-info defer-load]").select("table");
        Iterator < Element > iterator = table.select("ul li a").iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().text());
        }
        // second method
        for (Element tablee: doc.select("div[class=more-info defer-load]").select("table")) {
            for (Element row: tablee.select("tr")) {
                Elements tds = row.select("td");
                if (tds.size() > 0) {
                    System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
                }
            }
        }
    }
}

here is an example of the html code that i'm trying to extract (taken from a linkedin profile)

<table summary="Coordonnées en ligne">
   <tr>
      <th>E-mail</th>
      <td>
         <div id="email">
            <div id="email-view">
               <ul>
                  <li>
                     <a href="mailto:[email protected]">[email protected]</a>
                  </li>
               </ul>
            </div>
         </div>
      </td>
   </tr>
   <tr class="no-contact-info-data">
      <th>Messagerie instantanée</th>
      <td>
         <div id="im" class="editable-item">
         </div>
      </td>
   </tr>
   <tr class="address-book">
      <th>Carnet d’adresses</th>
      <td>
         <span class="address-book">
         <a title="Une nouvelle fenêtre s’ouvrira" class="address-book-edit" href="/editContact?editContact=&contactMemberID=368674763">Ajouter</a> des coordonnées.
         </span>
      </td>
   </tr>
</table>
<table summary="Coordonnées">
   <tr>
      <th>Téléphone</th>
      <td>
         <div id="phone" class="editable-item">
            <div id="phone-view">
               <ul>
                  <li>0021653191431&nbsp;(Mobile)</li>
               </ul>
            </div>
         </div>
      </td>
   </tr>
   <tr class="no-contact-info-data">
      <th>Adresse</th>
      <td>
         <div id="address" class="editable-item">
            <div id="address-view">
               <ul>
               </ul>
            </div>
         </div>
      </td>
   </tr>
</table>
like image 604
AMI Avatar asked Nov 08 '22 11:11

AMI


1 Answers

To scrape email and phone number, use css selectors to target the element identifiers.

    String email = doc.select("div#email-view > ul > li > a").attr("href");
    System.out.println(email);

    String phone = doc.select("div#phone-view > ul > li").text();   
    System.out.println(phone);

See CSS Selectors for more information.

Output

mailto:[email protected]
0021653191431 (Mobile)
like image 90
Zack Avatar answered Nov 14 '22 22:11

Zack