使用Jsoup在脚本标记内获取文本

时间:2017-07-20 12:05:39

标签: javascript java android jsoup

如何使用Jsoup获取值http://api.tivi8k.net/viettel/?cid=154&token=-OWPRmOicPqIKsK97SmOlQ&e=1500542988

enter image description here

HTML:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://api.tivi8k.net/viettel/?cid=154&token=-OWPRmOicPqIKsK97SmOlQ&e=1500542988", false);
xmlhttp.send();
link = xmlhttp.responseText;

player = new Clappr.Player({
  source: link,
  parentId: '#player',
  width: '100%',
  height: "100%",
  hideMediaControl: true,
  mediacontrol: {
    seekbar: "#ffaa56",
    buttons: "#ff7f00"
  },
  autoPlay: true
});
html,
body {
  margin: 0;
  padding: 0;
  background: #000;
  ;
}
<div style="width: 100%;">
  <div id="player"></div>
</div>

<img src="sv2.png" style="position:absolute;top:5px;right:10px" />

Java代码

try {
    String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
    Document doc = Jsoup.connect("http://m.xemtvhd.com/vtv1.php").userAgent(userAgent).get();

    String url = doc.select("#xmlhttp.open").first().attr("data-source");

    System.out.println(url);

} catch (IOException e) {
    e.printStackTrace();
}

请支持。感谢支持

1 个答案:

答案 0 :(得分:2)

此代码将获取所有<script>个元素并迭代每个元素,然后它将检查他看起来的那个是你想要的那个 pattern如果matches group(1)(代表捕获组(.*)group(0)是整行),则为Elements script = doc.select("script"); Pattern p = Pattern.compile("xmlhttp.open\\(\"GET\", \"(.*)\", false\\)"); // ^^ is the capturing group String url = ""; for (Element element : script) { Matcher m = p.matcher(element.data()); if (m.find()){ url = m.group(1); } } System.out.println(url); //-> http://...542988

seenModal