项目中用到了一个页面中的数据,做为项目中的接口数据返回。html页面中包含很多
<script></script>标签,并且有的中间写了脚本。这个脚本中获取数据,这个让我很费解。还有这种需求。
而且js中的变量 新建的时候是"";然后再给赋值。我要取出这个页面中这个脚本中的这个变量的具体内容来。
上代码
//省略了html中的部分代码
</div>
<script>
var title = '数据';
var categoryId = '888';
var module = 'P';
var productId = '9999';
// 起报日期列表
var dateList = [];
dateList.push({
actived: "actived",
time: "20210608",
tid: "",
text: "2021/06/08"
});
dateList.push({
actived: "",
time: "20210607",
tid: "",
text: "2021/06/07"
});
</script>
<script type="text/javascript">
var categoryId = "58378";
</script>
<script type="text/javascript">
$('title').text(title);
$(function () {
ImagePlayer.init({dateList: dateList, dataList: dataList, step:1, module:'P', orderBy:'desc'});
Nmc.getLikeCount('58378');
});
</script>
import javax.script.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.List;
import static java.lang.Integer.parseInt;
@SpringBootTest
public class TestDemo {
public static void main(String[] args) {
try {
StringBuffer buffer = new StringBuffer();
//读取桌面上的html文件
InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\data1.html"), "GBK");
BufferedReader bf = new BufferedReader(isr);
String s = "";
while((s = bf.readLine())!=null){
buffer.append(s.trim());
}
String xml = buffer.toString();
Document document = Jsoup.parse(xml);
int count = 0;
//取页面中所有的<script>标签
Iterator<Element> imgs = document.select("script").iterator();
while (imgs.hasNext()) {
Element img = imgs.next();
String data = img.data();
//根据关键字找到你想要执行的js脚本
if(data.contains("起报日期列表")) {
//如果js脚本中有中文的话不能执行,一定要替换成空格
data = data.replace("// 起报日期列表", " ");
data = data.replace("// 数据列表", " ");
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("javascript");
//执行当前脚本的方法
engine.eval(data);
//获取脚本中执行后的变量的的对象
Object dateList = engine.get("dateList");
Object dataList = engine.get("dataList");
Map aMap = new HashMap();
aMap = (Map) dataList;
List alist = new ArrayList();
List elist = new ArrayList();
Iterator<Map.Entry<String, Object>> it = aMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
alist.add(entry.getValue());
}
aMap.clear();
aMap = (Map) dateList;
Iterator<Map.Entry<String, Object>> it1 = aMap.entrySet().iterator();
while (it1.hasNext()) {
Map.Entry<String, Object> entry = it1.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
elist.add(entry.getValue());
}
Gson gson = new Gson();
aMap.clear();
aMap.put("dateList", elist);
aMap.put("dataList", alist);
String resJson = gson.toJson(aMap);
System.out.println(resJson);
}
}
}catch (Exception exception) {
exception.printStackTrace();
}
}