Java 使用JSoup CSS选择器

Java 使用JSoup CSS选择器,java,css-selectors,web-crawler,jsoup,Java,Css Selectors,Web Crawler,Jsoup,我试图用它从网站上刮下一些内容。以下是我感兴趣的页面中的一些HTML内容示例: <div class="sep_top shd_hdr pb2 luna"> <div class="KonaBody" style="padding-left:0px;"> <div class="lunatext results_content frstluna"> <div class="luna-Ent">

我试图用它从网站上刮下一些内容。以下是我感兴趣的页面中的一些HTML内容示例:

<div class="sep_top shd_hdr pb2 luna">
    <div class="KonaBody" style="padding-left:0px;">
        <div class="lunatext results_content frstluna">
            <div class="luna-Ent">
                <div class="header">
                <div class="body">
                    <div class="pbk">
                        <div id="rltqns">
                    <div class="pbk">
                        <span class="pg">
                            <span id="hotword">
                                <span id="hotword">Fizz</span>
                            </span>
                        </span>
                        <div class="luna-Ent">
                        <div class="luna-Ent">
                        <div class="luna-Ent">
                        <div class="luna-Ent">
                    </div>
                    <div class="pbk">
                        <span class="sectionLabel">
                        <span class="pg">
                            <span id="hotword">
                                <span id="hotword">Buzz</span>
                            </span>
                        </span>
                        <span class="pg">
                            <span id="hotword">
                                <span id="hotword">Foo</span>
                            </span>
                        </span>
                        <span class="pg">
                            <span id="hotword">
                                <span id="hotword">Bar</span>
                            </span>
                        </span>
                    </div>
                <div class="tail">
            </div>
            <div class="rcr">
        <!-- ... rest of content omitted for brevity -->
运行该代码将生成以下输出:

Starting to crawl...
Found 3 pbks.

我要么没有正确使用JSOUPAPI,要么没有使用正确的选择器,或者两者都没有。有没有想过我会在哪里出错?提前谢谢

如果您使用的是
getElementsByClass
,那么您不需要在它前面添加
,只需使用类名称,比如
getElementsByClass(“pg”)
,而不是
getElementsByClass(.pg”)

Elements hotwords = document.select("#hotwords");

for (Element hotword : hotwords){
    String word = hotword.getText();
}
这同样适用于
getElementById
。不要在
id
值之前添加
。只需使用
getElementById(“hotword”)

另外,您的
div
s和
pbk
类似乎是嵌套的,因此
getElementsByClass
可能会给您重复的结果


在知道您试图解析的页面之后,您可以使用一个选择器进行解析。也许这样试试

for (Element element:doc.select("div.body div.pbk span.pg")){
    System.out.println(element.text());
}

谢谢@William Falcon,但这也不行。
hotword
变量的大小为0。谢谢@Pschemo(+1)-这有点帮助,但现在它告诉我文档中没有hotwords,我知道这是错误的。我实际上想点击的URL是,我试图积累一个特定单词的所有不同“单词类型”(形容词、名词、动词)的列表。例如,在这个链接上,“快”一词有三种不同的类型:形容词、名词和副词。我如何调整我的JSoup选择器以获得一个包含“形容词”、“名词”和“动词”值的列表?@TicketMonster我稍微更新了我的代码。它看起来像你想要的那样工作。在看到JSoup从该站点获得的HTML代码后,我提出了这个解决方案(您可以通过
System.out.println(doc);
)看到它)。@TicketMonster没问题:)
for (Element element:doc.select("div.body div.pbk span.pg")){
    System.out.println(element.text());
}