Zen Tao api call (crawler method)

Table of contents

Get information about all ongoing projects

url

postman

java code

Entity class

logic processing

Get all unclosed task ids under the specified project according to the project id

url

postman

java code

Summarize


Get information about all ongoing projects

url

http://Zen Tao address xxx/zentao/project-all-doing-project ID-order_desc-0.html

postman

java code

You can refer to this blog about the business implementation of ZenTao login: http://t.csdn.cn/I2uPU

Entity class

package com.example.zentaoaibackend.entity;

import lombok.Data;
import java.util.Date;
import java.io.Serializable;

public class ProjectInfoEntity extends BaseAttribute {
    /**
     * 项目ID
     */
    private Integer projectId;

    /**
     * 项目名称
     */
    private String name;
    /**
     * 项目代号
     */
    private String code;
    /**
     * 项目负责人
     */
    private String director;
    /**
     * 项目截止日期
     */
    private Date endtime;
    /**
     * 是否参与消息推送
     */
    private int joinStatus;
    /**
     * 钉钉群id
     */
    private String dingGroupId;
    /**
     * 钉钉群名称
     */
    private String dingGroupName;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public Date getEndtime() {
        return endtime;
    }

    public void setEndtime(Date endtime) {
        this.endtime = endtime;
    }

    public int getJoinStatus() {
        return joinStatus;
    }

    public void setJoinStatus(int joinStatus) {
        this.joinStatus = joinStatus;
    }

    public String getDingGroupId() {
        return dingGroupId;
    }

    public void setDingGroupId(String dingGroupId) {
        this.dingGroupId = dingGroupId;
    }

    public String getDingGroupName() {
        return dingGroupName;
    }

    public void setDingGroupName(String dingGroupName) {
        this.dingGroupName = dingGroupName;
    }

    public void setProjectId(Integer projectId) {
        this.projectId = projectId;
    }

    public Integer getProjectId() {
        return projectId;
    }

}

logic processing

The format of the html page returned by crawling is as shown in the figure below, and we analyze it according to the given structure

import com.example.zentaoaibackend.entity.ProjectInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


public List<ProjectInfoEntity> GetDoingProjectInfo() {
        List<ProjectInfoEntity> doingProjectList = new ArrayList<>();
        try {
            //登录禅道
            zentaoLogin();

            //url地址
            String url = "http://127.0.0.1/zentao/project-all-doing-422-order_desc-0.html";
            //执行url,并传入cookie
            Document doc = Jsoup.connect(url).cookie("zentaosid", Constant.ZENTAOID).get();
            //解析html页面,获取tr标签
            Elements tfElements = doc.select("table").select("tbody").select("tr");

            //遍历tr标签,根据任务状态获取对应的任务id,并放入到集合中
            for (Element element : tfElements) {
                //获取td标签
                Elements tdElements = element.select("td");
                //用于解析后的数据信息存储
                ProjectInfoEntity projectInfo = new ProjectInfoEntity();

                //遍历td标签
                for (Element tdElement : tdElements) {
                    //判断当前遍历的标签中class元素是否包含text-left
                    //包含:获取项目id
                    //不包含:继续下一次遍历
                    if (tdElement.attr("class").equals("text-left")) {
                        //获取项目ID
                        String attr = element.attr("data-id");
                        //将项目ID传入到实体中
                        projectInfo.setProjectId(Integer.valueOf(attr));

                        //因为返回的有多个td标签中都包含了class='text-left'的元素,我们不能直接获取项目信息,需要进行判断
                        //包含title元素:为项目代码;否则:为项目名称
                        if (tdElement.attr("title").equals("")) {
                            //项目代号
                            projectInfo.setCode(tdElement.text());
                        } else {
                            //项目名称
                            projectInfo.setName(tdElement.attr("title"));
                        }
                    }
                }
                doingProjectList.add(projectInfo);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return doingProjectList;
    }


Get all unclosed task ids under the specified project according to the project id

url

http://Zen Tao address xxx/zentao/project-task-439-unclosed.html

postman

java code

package com.example.zentaoaibackend.utils.zentaoAPI;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.example.zentaoaibackend.utils.zentaoAPI.DoGet.doGet;
import static com.example.zentaoaibackend.utils.zentaoAPI.login.ZentaoLogin.zentaoLogin;


/**
 * @BelongsProject: zentaoai-backend
 * @BelongsPackage: com.example.zentaoaibackend.utils.zentaoAPI
 * @Author: author
 * @CreateTime: 2022-11-26  11:14
 * @Description: 根据项目id获取指定项目下所有任务
 * @Version: 1.0
 */
@Component
public class GetProjectTask extends IJobHandler {
    public List<String> GetTaskByProjectId(String projectId) throws IOException {
        List<String> unClosedList = new ArrayList<>();
        try {
            zentaoLogin();
            //url地址
            String url = "http://127.0.0.1/zentao/project-task-" + projectId + "-unclosed.html";
            //执行接口调用,并传入cookie
            Document doc = Jsoup.connect(url).cookie("zentaosid", Constant.ZENTAOID).get();
            //解析html页面,获取tr标签
            Elements select = doc.select("table").select("tbody").select("tr");
            //遍历tr标签,根据任务状态获取对应的任务id,并放入到集合中
            for (Element element : select) {
                unClosedList.add(element.attr("data-id"));
            }

        } catch (Exception e) {
            System.out.println(e);
        }
        return unClosedList;
    }

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        return null;
    }
}

Summarize

Note: Individual information is relatively private and fuzzy, and it is not easy to disclose, please forgive me! !

If you want to exchange content, please leave a message in the comment area. If this document is liked by you, please leave your likes and collection footprints to support the blogger~

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/128426836