使用jsoup检索html中的span值

时间:2018-06-21 00:03:08

标签: java html jsoup

我正在尝试从以下html片段的“ display-ratings”跨度类中检索数字“ 1”:

 <article class="surveys-verbatim__item ">
    <h3 id="engine_major">
        <a class="surveys-verbatim__item-title surveys-verbatim__item-title--font-large crux-body-copy crux-body-copy--bold" data-cr-collapse data-target=".surveys-verbatim__item" href="javascript:void(0);">
            Engine Major
            <span class="crux-icons crux-icons-caret-up-small surveys-verbatim__item__arrow"></span>
        </a>
    </h3>
    <div class="surveys-verbatim__item-body">
        <div class="row">
            <div class="col-sm-6 col-xs-12">
                <p class="crux-body-copy crux-body-copy--small surveys-verbatim__item-description">Engine rebuild or replacement, cylinder head, head gasket, turbo or supercharger, timing chain or timing belt.</p>
            </div>
                                <div class="col-md-offset-2 col-md-4 col-sm-offset-1 col-sm-5 col-xs-12">
                    <div class="surveys-verbatim__item-ratings">
                        <div class="brick-chart-chart-container">
    <div class="brick-chart-chart-wrapper-container clearfix">
<div class="brick-chart-chart-wrapper poor">
    <div class="brick-chart-brick"></div>
    <div class="brick-chart-brick active" style="width: 20%"></div>
</div>

<div class="score-range ">
                <span><span class="display-ratings">1</span> / 5</span>
        </div>
</div>
</div>
                    </div>
                </div>
                        </div>
                </div>
</article>

我正在Java中使用Jsoup,并具有以下代码:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Main {

public static void main(String[] args) throws Exception {

    final Document document = Jsoup.connect("https://www.consumerreports.org/cars/audi/a6/2006/reliability?pagestop").get();
    for (Element element : document.select("article.surveys-verbatim__item")) {
        String rating = element.select("span.display-ratings").text();
        System.out.println(rating);
    }

  }
}

此输出空白字符串。谁能告诉我他们是否知道如何提取跨度类“ display-ratings”中列出的值?

谢谢!

1 个答案:

答案 0 :(得分:0)

您要提取的等级仅在登录网站后可见。 Jsoup解析了该URL,但是由于它是隐藏的而无法提取评级。

如果要获得评级,则必须以编程方式登录到网站并解析该网页。

Here,这是登录前页面源的屏幕截图。您可以看到评级已在此处锁定,即在此处不可见。