【问题标题】:Extract Heading with JSoup使用 JSoup 提取标题
【发布时间】:2015-10-12 00:43:16
【问题描述】:

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

谁能帮帮我,这段代码没有提供任何输出

【问题讨论】:

    标签: java web-scraping jsoup extract


    【解决方案1】:

    试试h2.product-description__title

    你可以在这里看到它的实际效果:http://try.jsoup.org/~Mkx5qLNvIT2cxUHdNLl9vdTUnYM

    【讨论】:

    • 检查我发布的链接,它显示了一个 css 查询工作的示例。
    • 哦,但是我尝试了编码,仍然没有输出
    • 任何人请帮助我为什么这个编码没有给出任何输出
    • Damien 的代码运行良好。编辑您的问题并添加您的整个代码,也许我们可以找到问题。
    【解决方案2】:

    看起来服务器需要一个有效的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());
    

    编辑
    我已经加价了。

    【讨论】:

    • 它给出了输出但缺少价格
    • String price = doc.select("[class=product-price]").text(); System.out.println("价格为:" + 价格);
    • 您想要得到的 EXECT 输出是什么?
    • 基本上我想要价格和描述,当我输入你给它的代码时不会给出价格输出。
    • 非常感谢@TDG 的帮助,你能教我你是怎么知道的吗?非常感谢
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多