How to get text & Other tags between specific tags using Jericho HTML parser?

青春壹個敷衍的年華 提交于 2019-12-13 12:37:57

问题


I have a HTML file which contains a specific tag, e.g. <TABLE cellspacing=0> and the end tag is </TABLE>. Now I want to get everything between those tags. I am using Jericho HTML parser in Java to parse the HTML. Is it possible to get the text & other tags between specific tags in Jericho parser?

For example:

<TABLE  cellspacing=0>    
  <tr><td>HELLO</td>  
  <td>How are you</td></tr>
</TABLE>

Answer:

<tr><td>HELLO</td>  
<td>How are you</td></tr> 

回答1:


Once you have found the Element of your table, all you have to do is call getContent().toString(). Here's a quick example using your sample HTML:

Source source = new Source("<TABLE  cellspacing=0>\n" +
    "  <tr><td>HELLO</td>  \n" +
    "  <td>How are you</td></tr>\n" +
    "</TABLE>");

Element table = source.getFirstElement();
String tableContent = table.getContent().toString();

System.out.println(tableContent);

Output:

    <tr><td>HELLO</td>  
    <td>How are you</td></tr>



回答2:


Aby, I walk down the code for all elements and show on screen. Maybe help you.

        List<Element> elementListTd = source.getAllElements(HTMLElementName.TD);

        //Scroll through the list of elements "td" page
        for (Element element : elementListTd) {
            if (element.getAttributes() != null) {
                String td = element.getAllElements().toString();
                String tag = "td";
                System.out.println("TD: " + td);
                System.out.println(element.getContent());
                String conteudoAtributo = element.getTextExtractor().toString();
                System.out.println(conteudoAtributo);

                if (td.contains(palavraCompara)) {
                    tabela.add(conteudoAtributo);
                }

            }


来源:https://stackoverflow.com/questions/5625888/how-to-get-text-other-tags-between-specific-tags-using-jericho-html-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!