这里查询到的项目名称可能和我们的项目名称出入较大,需要做个黑名单过滤。比如人工判断查出来的项目名字不是相同的,就加个黑名单,下次是相同名字的就不再保存。
public List<ProjectRecord> htmlParser(String projectName) {
List<ProjectRecord> recordList = new ArrayList<>();
//网页github链接
String url = "https://github.com/search?l=Java&p=1&q=" + projectName + "&type=Repositories";
Document document = null;
try {
document = Jsoup.connect(url).timeout(30000).get();
Element element = document.body();
//查找div标签下面的div标签下面的div标签下面的a标签里面的.class属性为v-align-middle的文本内容
Elements div = element.select("div>div>div>a[class=v-align-middle]");
if (div.size() == 0) {
return null;
}
ProjectRecord record = new ProjectRecord();
//获取标签里面的内容
Element d = div.get(1);
record.setProjectLinks(url);
record.setProjectName(d.text());
recordList.add(record);
} catch (Exception e) {
e.printStackTrace();
}
return recordList;
}
public class ProjectRecord {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
/**
* 监控的项目链接
*/
private String projectLinks;
/**
* 监控的项目名称
*/
private String projectName;
}