java Jsoup 在 value="" 中获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7546162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jsoup get element in value=" "
提问by Lars
I want to find the element "buddyname" and get the element of value= "" in a HTML file which i put into a StringBuffer, in this case 5342test. The element in value= "" can change so i can not search directly for 5342test.
我想在我放入 StringBuffer 的 HTML 文件中找到元素“buddyname”并获取 value="" 元素,在本例中为 5342test。value="" 中的元素可以更改,因此我无法直接搜索 5342test。
<fieldset style="display:none"><input type="hidden" name="buddyname" value="5342test"/></fieldset>
How can i do this with jsoup? or is there an easier way, I already tried Pattern/Matcher but that did not work out as i had issues with the Pattern.compile("<input[^>]*?value\\s*?=\\s*?\\\"(.*?)\\\")");
我怎样才能用 jsoup 做到这一点?或者有没有更简单的方法,我已经尝试过 Pattern/Matcher,但是没有成功,因为我遇到了问题Pattern.compile("<input[^>]*?value\\s*?=\\s*?\\\"(.*?)\\\")");
Below some example code. Thank you in advance.
下面是一些示例代码。先感谢您。
Document doc = Jsoup.parse(page); // page is a StringBuffer
Elements td = doc.select("fieldset");
for (Element td : tds) {
String tdText = td.text();
System.out.println(tdText);
}
回答by BalusC
Just use the attribute selector [attrname=attrvalue]
.
只需使用属性选择器[attrname=attrvalue]
。
Element buddynameInput = document.select("input[name=buddyname]").first();
String buddyname = buddynameInput.attr("value");
// ...
Do not use regex to parse HTML. It makes no sense if you already have a world class HTML parser at your hands.
不要使用正则表达式来解析 HTML。如果您手头已经拥有世界一流的 HTML 解析器,那将毫无意义。