使用 Jsoup 从页面源中提取单个值

Using Jsoup to extract single value from page source

我只需要从网页中提取一个值。该值是每次访问页面时生成的随机数。我不会 post 整页源代码,但包含该值的字符串是:

            <span class="label label-info pull-right">Expecting 937117</span>

“937117”是我在这里寻找的值。谢谢

更新

这是我目前得到的:

    Document doc = Jsoup.connect("www.mywebsite.com).get();
    Elements value = doc.select("*what do I put in here?*");

    System.out.println(value);

能不能不用javascript正则表达式语法?如果你知道你感兴趣的元素,从 jsoup 中将它作为字符串 $stuff 提取出来,然后就可以了 $stuff.match( /期望 (\d*)/ )[1]

以下代码段中对所有内容进行了清楚的描述。我创建了一个 HTML 文件,里面有一个类似的 SPAN 标签。使用 Document.select() 到 select 具有您想要的特定 class 名称的元素。

import java.io.File;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Entities.EscapeMode;
import org.jsoup.select.Elements;

public static void main(String[] args) {
    String sourceDir = "C:/Users/admin/Desktop/test.html";
    test(sourceDir);
}

private static void test(String htmlFile) {
    File input = null;
    Document doc = null;
    Elements classEles = null;

    try {
        input = new File(htmlFile);
        doc = Jsoup.parse(input, "ASCII", "");
        doc.outputSettings().charset("ASCII");
        doc.outputSettings().escapeMode(EscapeMode.base);

        /** Find all SPAN element with matched CLASS name **/
        classEles = doc.select("span.label.label-info.pull-right");

        if (classEles.size() > 0) {
            String number = classEles.get(0).text();
            System.out.println("number: " + number);
        }
        else {
            System.out.println("No SPAN element found with class label label-info pull-right.");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
        public void yourMethod() {

          try {
                Document doc = connect("http://google.com").userAgent("Mozilla").get();
                Elements value = doc.select("span.label label-info pull-right");

              } catch (IOException e) {
                e.printStackTrace();
              }
         }