可以使用 Java 的 HTML 解析库 Jsoup,通过选择器选择 select 元素并遍历 option 子元素获取数据。

示例代码:

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 {
        // 获取页面数据
        String url = 'https://www.example.com';
        Document doc = Jsoup.connect(url).get();
        
        // 选择 select 元素
        Element select = doc.selectFirst('select[name=example]');
        
        // 遍历 option 子元素获取数据
        Elements options = select.select('option');
        for (Element option : options) {
            String value = option.val();
            String text = option.text();
            System.out.println(value + ' - ' + text);
        }
    }
}

其中,选择器 select[name=example] 表示选择元素名为 select,属性名为 name,属性值为 example 的元素。 option 表示选择所有 option 元素。 val() 方法获取 option 元素的 value 属性值, text() 方法获取 option 元素的文本内容。

Java 获取页面 Select 元素数据 (不使用表单)

原文地址: https://www.cveoy.top/t/topic/nzo2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录