Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsoup:从一段javascript解析html_Java_Jsoup - Fatal编程技术网

Jsoup:从一段javascript解析html

Jsoup:从一段javascript解析html,java,jsoup,Java,Jsoup,你们中有人知道如何使用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&g

你们中有人知道如何使用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>')"

您可以通过找到
onmouseover
属性,然后处理获得的字符串(在下面的示例中,我使用regex)以获得所需的参数值:

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);
    }
}
import java.util.regex.*;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.*;
公共类JSoupGetAttributeExample{
公共静态void main(字符串[]args){
Document doc=Jsoup.parse(“示例”+
"" +
"   " +
"       "+
"       "+
"   " +
"" +
"");
元素myTable=doc.getElementById(“myTable”);
字符串onmouseover=myTable.attr(“onmouseover”);
System.out.println(“onmouseover属性:+onmouseover”);
/*字符串处理以获取HTML(第二个)参数*/
字符串参数=null;
Pattern p=Pattern.compile(“showHoverInfo\\('.''.','(.*?'\\)”;
匹配器m=p.Matcher(onmouseover);
if(m.find()){
第二参数=m组(1);
}
System.out.println(“\nHTML参数:“+secondParameter”);
}
}
输出:

onmouseover属性:showHoverInfo(“”,“”)
HTML参数:

否,matcher找不到HTML属性。当您运行我的示例或在实际页面中运行时?感谢您的提议,但我已经用另一种方式解决了我的问题。我真的很感激你的提议!啊,太好了!至于这个问题,为什么不接受呢?它没有返回html属性吗?我很好奇。不,它没有返回html属性。