0

你们中有人知道如何使用 Jsoup 从 javascript onmouseover 事件中获取 html 吗?这听起来可能很模糊,所以这里是代码:

<table onmouseover="showHoverInfo('', '<a href="somelink"><b>sometext</b>/a><br /> Some other text <br /> <a href="some other link"><b>Some text</b></a>')"

它还在继续。我想知道的是:如何使用 Jsoup 从 showHoverInfo() 方法中获取 html 代码?

感谢所有帮助。

4

1 回答 1

2

您可以通过找到该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>
于 2013-06-29T09:03:04.230 回答