java - How to get children of class in jsoup -


i want scrape comment website. having trouble p tag inside class in jsoup. example html code below

<html>  <head>   <title>my webpage</title>  </head>  <body>   <div class="container">      <div class="comment">       <p>this comment</p>      </div>   </div>  </body>  </html>  

here java code

public static void main(string args[]){     document doc = null;     try {          doc = jsoup.connect("https://homeshopping.pk/products/amazon-fire-phone-%284g%2c-32gb%2c-black%29-price-in-pakistan.html").get();         system.out.println("connect successfully");         org.jsoup.select.elements element =  doc.select("div.post-message");          system.out.println(element.get(0).text());     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     }  } } 

the comments section of page trying fetch not simple html contant. comments loaded dom javascript after initial page load. jsoup html parser, can not fetch comments of page jsoup. fetch kind of content need embedded browser component. take @ answer : is there way embed browser in java?

the below code specific html string provided.

try this:

import org.jsoup.jsoup; import org.jsoup.nodes.document; import org.jsoup.select.elements;     public class test {     public static void main(string[] arg) {      document doc = null;      try {           doc = jsoup.parse("<html> "                 + "<head>  "                 + "<title>my webpage</title> "                 + "</head> <body>  <div class=\"container\">     "                 + "<div class=\"comment\">      "                 + "<p>this comment</p>    "                 + " </div>  </div> </body></html> ");                  elements element = doc.select(".container").select(".comment");                  system.out.println(element.get(0).select("p").text());       }      catch (exception e)      {          e.printstacktrace(); }   }    } 

for connecting url use :

doc = jsoup.connect("https://homeshopping.pk/products/amazon-fire-phone-%284g%2c-32gb%2c-black%29-price-in-pakistan.html").timeout(60*1000).useragent("mozilla").get(); 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -