使用JSoup提取标题

时间:2015-10-09 15:46:15

标签: java web-scraping jsoup extract

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);

任何人都可以帮助我,这段代码不提供任何输出

2 个答案:

答案 0 :(得分:1)

尝试h2.product-description__title

您可以在此处看到它:http://try.jsoup.org/~Mkx5qLNvIT2cxUHdNLl9vdTUnYM

答案 1 :(得分:0)

看起来服务器需要一个有效的UserAgent字符串。将其添加到代码中:

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());

修改
我加了价格。