如何使用java获取页面中selectoption中的数据不使用表单
可以使用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元素的文本内容
原文地址: https://www.cveoy.top/t/topic/cKKl 著作权归作者所有。请勿转载和采集!