在HTML页面中,我想选择javascript变量的值。以下是HTML页面的片段。
<script id="page-data">
var __IS_MIRA__;
var __INITIAL_STATE__ = undefined;
var __CACHE_REGISTRY__ = undefined;
var __NEXT_CACHE_ID__ = undefined;
var __DMP_CONFIG__ = {"context":{"access_token":null,"ad_sync_script_url":"http:\/\/www.taolao.com\/cdn\/manifest\/video\/x7775n8.m3u8?auth=1498553714-2562-k2kou1s3-7be1a0645b68824508f7f4989900d487yk2kou1s3&bs=1","admin":false,"as_number":"AS18403","user":null}
我的目标是使用jsoup从此页面读取变量 DMP_CONFIG 的值。 jsoup有可能吗?如果是,那怎么样?
这是我的Java代码。
Document doc = Jsoup.connect(""+urlhtml).get();
Element div = doc.getElementById("page-data");
Pattern p = Pattern.compile("(?is) __DMP_CONFIG__ = \"(.+?)\""); // Regex for the value of the key
Matcher m = p.matcher(div.html());
while( m.find() ) {
mData =m.group(1);
}
答案 0 :(得分:0)
在这种情况下,您应该使用正则表达式来匹配组结果。
ad_sync_script_url":"([^"]+)"
示例代码如下:
String REGEX = "ad_sync_script_url":"([^"]+)"";
Pattern r = Pattern.compile(REGEX);
Matcher m = r.matcher("String that you want to match"); // get a matcher object
if(m.find()){
String matchValue = m.group(0);
String result = matchValue.Replace("\"","");
}else{
//doesn't match result.
}
希望这有帮助:)