Java - Obtain text within script tag using Jsoup

后端 未结 4 1981
渐次进展
渐次进展 2021-01-01 12:19

I am using the Jsoup library to read a URL. This url has text within a few

4条回答
  •  粉色の甜心
    2021-01-01 12:30

    Yes. You can use Element#getElementsByTag() to get all the script tag . Each script tags will be represented by the DataNode.

     Document doc =Jsoup.connect("http://stackoverflow.com/questions/16780517/java-obtain-text-within-script-tag-using-jsoup").timeout(10000).get();
     Elements scriptElements = doc.getElementsByTag("script");
    
     for (Element element :scriptElements ){                
            for (DataNode node : element.dataNodes()) {
                System.out.println(node.getWholeData());
            }
            System.out.println("-------------------");            
      }
    

提交回复
热议问题