解析HTML页面数据工具类

项目中用到了一个页面中的数据,做为项目中的接口数据返回。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();
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值