简体   繁体   中英

Extract Heading with JSoup

HTML

<div class="product-description__block">
    <h2 class="product-description__title">Product details of Asus Zenfone C ZC451CG 16GB (White)</h2>
    Asus once again provides gadget hungry users with another quality product, the Asus Zenfone C.<br><br>

Jsoup

String url = "http://www.lazada.com.my/asus-zenfone-c-zc451cg-16gb-white-2801812.html";
Document doc = Jsoup.connect(url).get();
Elements description = doc.select("h2.product-description__title");
System.out.println("Description :"+description);

can anyone help me, this code doesn't provide any output

Try h2.product-description__title

You can see it in action here: http://try.jsoup.org/~Mkx5qLNvIT2cxUHdNLl9vdTUnYM

It looks like the server requiers a valid UserAgent string. Add this to the code:

String url = "http://www.lazada.com.my/asus-zenfone-c-zc451cg-16gb-white-2801812.html";
Document doc = Jsoup.connect(url)
       .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1")
       .get();
Elements description = doc.select("h2.product-description__title");
System.out.println("Description :"+description);
System.out.println("Price is " + doc.select("#pdtprice").text());

Edit
I've added the price.

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.

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