您可以通过找到该onmouseover
属性.attr()
,然后处理获得的字符串(在下面的示例中我使用正则表达式)以获得您想要的参数值:
import java.util.regex.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
public class JSoupGetAttributeExample {
public static void main(String[] args) {
Document doc = Jsoup.parse("<html><body><div>example</div>" +
"<table id='myTable' onmouseover=\"showHoverInfo('', '<a href=\\\'somelink\\\'><b>sometext</b>/a><br /> Some other text <br /> <a href=\\\'some other link\\\'><b>Some text</b></a>')\" >" +
" <tr>" +
" <td>"+
" </td>"+
" </tr>" +
"</table>" +
"</body></html>");
Element myTable = doc.getElementById("myTable");
String onmouseover = myTable.attr("onmouseover");
System.out.println("onmouseover ATTRIBUTE: "+onmouseover);
/* String processing to get the HTML (second) parameter */
String secondParameter = null;
Pattern p = Pattern.compile("showHoverInfo\\('.*', '(.*?)'\\)");
Matcher m = p.matcher(onmouseover);
if (m.find()) {
secondParameter = m.group(1);
}
System.out.println("\nHTML PARAMETER: "+secondParameter);
}
}
输出:
onmouseover ATTRIBUTE: showHoverInfo('', '<a href=\'somelink\'><b>sometext</b>/a><br /> Some other text <br /> <a href=\'some other link\'><b>Some text</b></a>')
HTML PARAMETER: <a href=\'somelink\'><b>sometext</b>/a><br /> Some other text <br /> <a href=\'some other link\'><b>Some text</b></a>