jevendavid

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
博客班级 https://edu.cnblogs.com/campus/fzzcxy/2018SE1
作业要求 https://edu.cnblogs.com/campus/fzzcxy/2018SE1/homework/11110
作业目标 推送和管理版本代码,完成网页的数据抓取
作业源代码 https://gitee.com/jevenDavid/personal
学号 211806403

1.代码行数:88行,分析需求25分钟,编码时间6小时。


2.思路:要算云班课平台的成绩,首先就要抓取云班课的经验值数据,通过jsoup筛选各个标签,通过属性值和顺序提取出需要的数据,最后存入变量中。

  • 先导入需要解析的网页
  • 然后导入配置文件
  • 再设置筛选条件将数据提取出来
  • 数据放到变量当中计算。
    通过以上系统模块化的方式能够让代码可阅读性提升。

public class Score {
    public static void main(String args[]) throws IOException {
        Document all=input( "D:/htmlSave/all.html");
        Document small=input("D:/htmlSave/small.html");
        System.out.println(String.format("%.2f",Calculator(all)));
        System.out.println(String.format("%.2f",Calculator(small)));
    }

    public static Document input(String Html) throws IOException {
        //找到本地html文件并导入
        File html = new File(Html);
        Document doc = Jsoup.parse(html, "UTF-8");
        return doc;
    }
    public static double Calculator(Document file1) throws IOException {
        //初始化参数
        int before=0;
        int base=0;
        int test=0;
        int program=0;
        int add=0;
        Properties properties=new Properties();//导入配置文件
        properties.load(new FileInputStream("src/total.properties"));
        //导入配置文件中的参数
        double t_before=Integer.parseInt(properties.getProperty("before"));
        double t_base=Integer.parseInt(properties.getProperty("base"));
        double t_test=Integer.parseInt(properties.getProperty("test"));
        double f_program=Integer.parseInt(properties.getProperty("program"));
        double f_add=Integer.parseInt(properties.getProperty("add"));
        Elements element=file1.getElementsByAttributeValue("class","interaction-row");//筛选
        if(file1!=null){
            for(Element e:element){
                if (e.child(1).child(0).toString().contains("课前自测")){
                    if (e.child(1).child(2).toString().contains("color:#8FC31F\"")){
                        Scanner scanner=new Scanner(e.child(1).child(2).children().get(0).children().get(10).text());
                        before+=scanner.nextInt();
                    }
                }else if (e.child(1).child(0).toString().contains("课堂完成")){
                    if (e.child(1).child(2).toString().contains("已参与 ")){
                        Scanner scanner=new Scanner(e.child(1).child(2).children().get(0).children().get(7).text());
                        base+=scanner.nextInt();
                    }
                }else if (e.child(1).child(0).toString().contains("课堂小测")){
                    if (e.child(1).child(2).toString().contains("已参与 ")){
                        Scanner scanner=new Scanner(e.child(1).child(2).children().get(0).children().get(7).text());
                        test+=scanner.nextInt();
                    }
                }else if (e.child(1).child(0).toString().contains("编程题")){
                    if (e.child(1).child(2).toString().contains("已参与 ")){
                        Scanner scanner=new Scanner(e.child(1).child(2).children().get(0).children().get(7).text());
                        program+=scanner.nextInt();
                    }
                }else if (e.child(1).child(0).toString().contains("附加题")){
                    if (e.child(1).child(2).toString().contains("已参与 ")){
                        Scanner scanner=new Scanner(e.child(1).child(2).children().get(0).children().get(7).text());
                        add+=scanner.nextInt();
                    }
                }
            }
        }
        //计算总分
        double r_before = before / t_before * 100 * 0.25;
        double r_base = base / t_base * 100 * 0.3 * 0.95;
        double r_test = test / t_test * 100 * 0.2;
        double r_program = program / f_program * 100 * 0.1;
        double r_add = add / f_add * 100 * 0.05;
        double t_score = (r_add + r_base + r_before + r_program + r_test) * 0.9 + 6;
        return t_score;
    }

}

3.首先遇到的困难就是抓取网页数据,网页是由html代码构成的,节点繁多,需要观察所需数据的位置。并且我并不知道哪个jar包能抓取网页数据,通过查找发现jsoup可以抓取网页数据,并且函数和js的代码是一样的。


4.参考链接:https://blog.csdn.net/yfx000/article/details/56831023


5.通过这次作业我觉得我更应该加强自我学习的能力。




posted on 2020-09-10 23:17  jevendavid  阅读(177)  评论(1编辑  收藏  举报