Why Jsoup cannot select td element?

后端 未结 3 1665
执笔经年
执笔经年 2021-01-25 01:21

I have made little test (with Jsoup 1.6.1):

String s = \"\" +Jsoup.parse(\"\").select(\"td\").size();
System.out.println(\"Selected elements         


        
相关标签:
3条回答
  • 2021-01-25 01:59

    Jsoup 1.6.2 allows to parse with different parser and simple XML parser is provided. With following code I could solve my problem. You can later parse your fragment with HTML parse, to get valid HTML.

    // Jsoup 1.6.2
    String s = "" + Jsoup.parse("<td></td>", "", Parser.xmlParser()).select("td").size();
    System.out.println("Selected elements count : " + s);
    
    0 讨论(0)
  • 2021-01-25 02:05

    Because Jsoup is a HTML5 compliant parser and you feeded it with invalid HTML. A <td> has to go inside at least a <table>.

    int size = Jsoup.parse("<table><td></td></table>").select("td").size();
    System.out.println("Selected elements count : " + size);
    
    0 讨论(0)
  • 2021-01-25 02:05
    String url = "http://foobar.com";
    Document doc = Jsoup.connect(url).get();
    Elements td = doc.select("td");
    
    0 讨论(0)
提交回复
热议问题