使用JSoup时选择具有多个类的Elements

使用JSoup时选择具有多个类的Elements

本文介绍了使用JSoup时选择具有多个类的Elements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从网站上解析一些表,特别是我正在尝试按类名提取以下单元格:

I am parsing some tables from a website, and specifically I am trying to extract the following cells by class name:

Elements e=d.select("span[class=bld lrg red]");

for (Element element : e) {
System.out.println(element.text());}

这段代码为我提供了一些值,这些值在网站中以Price的形式给出.但在某些情况下,我想从其他类别而不是"span[class=bld lrg red]"那里获取价格.

this code is giving me some values which is given as Price in the web site. but some cases, I want to take price from a different class rather than "span[class=bld lrg red]".

我的意思是"bld lrg红色"类为空,那么我想从"span [class = price]"中获取价值

在这种情况下,如何使用或".我的意思是,如果"bld lrg red"类具有值,则采用该价格或采用"price"类值.

How can I use 'or' in this case.. I mean if the "bld lrg red" class has value then take that price or take the "price" class value.

推荐答案

您也可以使用regex使用jsoup从网页中选择所需的元素,在那里您可以使用"OR"条件来指定您要寻找的内容;

You can also use regex to select the required elements in from your web page using jsoup too, there you can use and 'OR' condition to specify what you are looking for;

示例:

Elements e = d.select("span[class~=(?i)(bld lrg red|price)]");

上述正则表达式将使用与bld lrg redprice(不区分大小写)直接匹配的class选择器选择span元素.

The above regex would select your span elements with classselector directly matching bld lrg red OR price (case insensitive).

有关详细信息,请参见此处: http://jsoup.org/apidocs/org/jsoup/select/Selector.html

Refer to here for details: http://jsoup.org/apidocs/org/jsoup/select/Selector.html

现在,您可能要遍历元素,然后选择不为空白,为null的元素,等等.

Now you may want to iterate over the elements and select which are not blank, null, both and so on.

编辑:根据注释,price class不会被span元素保留.要克服它,您可以使用:

As per the comments, price class is not held by a span element. To get over it, you can use:

Elements e = d.select("span[class=bld lrg red],del[class=price]");

这篇关于使用JSoup时选择具有多个类的Elements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:19