Jsoup div [class =]语法有效,而div.class语法无效-为什么? [英] Jsoup div[class=] syntax works whereas div.class syntax doesn't - Why?

查看:96
本文介绍了Jsoup div [class =]语法有效,而div.class语法无效-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下HTML代码段:

For the following HTML snippet:

<div class="class_one class_two class_three classfour classfive classsix">
some inner content
</div>

以下Jsoup选择器有效:

The following Jsoup selector works:

div[class=class_one class_two class_three classfour classfive classsix]

但是等效的div.class语法不起作用:

But the equivalent div.class syntax doesn't work:

div.class_one.class_two.class_three.classfour.classfive.classsix

为什么?我想念什么?

编辑:根据下面收到的反馈,我意识到我无法解释无效"的含义.这是由于我对多类选择语法的工作方式感到困惑. 不起作用"是指上面的.classname语法比class=classname语法选择的div太多(具有相同的类名准确数目和相同的顺序!),因为所讨论的HTML包含附加的div.带有第7个类别名称...事实证明,这是设计 .那就是我所缺少的,这要归功于@Hovercraft Full Of Eels和@BalusC,它们帮助我发现了这一点.

EDIT: Based on the feedback I received below, I realize that I failed to explain what "doesn't work" means. This was due to my confusion as to how multiclass selection syntax works. By "not working" I meant that the .classname syntax above selects way too many divs than the class=classname syntax (with the same exact number of classname and in the same order!) does, because the HTML in question contained additional divs with a 7th class name... It turns out that this is by design. That's what I was missing, and thanks to @Hovercraft Full Of Eels and @BalusC who helped me discover this.

推荐答案

再次,根据我的评论,您需要在上下文中向我们展示您的代码,以显示其不起作用.

Again, as per my comment, you need to show us your code in context to show how it isn't working.

例如,当我尝试分析此简单文本时:

For instance when I try to analyze this simple text:

<html>
 <head></head>
 <body>
  <div class="class_one class_two class_three classfour classfive classsix">
    some inner content 
  </div> 
 </body>
</html>

使用以下代码:

import java.io.IOException;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Foo {
   public static void main(String[] args) throws IOException {
      Scanner scan = new Scanner(Foo.class.getResourceAsStream("Foo.txt"));
      String text = "";
      while (scan.hasNextLine()) {
         text += scan.nextLine() + "\n";
      }
      Document doc = Jsoup.parse(text);

      Elements eles = doc.select("div.class_one.class_two.class_three.classfour.classfive.classsix");
      System.out.println(eles);
   }
}

我得到这个结果:

<div class="class_one class_two class_three classfour classfive classsix">
  some inner content 
</div>

建议您使用选择的应该有效,如果不起作用,则可能正在发生其他情况.最好的选择是做我刚刚做的事情:发布一些数据和一些可编译的可运行代码( SSCCE ),然后让它显示您的代码是如何工作的.

Suggesting that your use of select should work, and if it's not working, something else may be going on. Your best bet may be to do what I've just done: post some data and some compilable runnable code (an SSCCE) and have it show just how your code is not working.

这篇关于Jsoup div [class =]语法有效,而div.class语法无效-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆