I'm using Jsoup to connect and to fetch all neccessary data to show it on my portfolio website. Now, I'm stuck trying to fetch attribute. I tried using .select("div.span10.offset h2") but that didn't work. This is my first time building a full-stack application so please dont be judgmental as there is bunch of sausage code.
This is my code
package com.example.Portfolio.services;
import com.example.Portfolio.consts.Constants;
import com.example.Portfolio.entities.projects.ProjectsDTO;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;
@Service
public class ProjectService{
static Logger logger;
static Constants consts = new Constants();
public static Set<ProjectsDTO> getProjectsByTitle(String projectName) {
Set<ProjectsDTO> projectsDTOS = new HashSet<>();
try {
Document document = Jsoup.connect(consts.url).get();
Elements elements = document.getElementsByTag("a");
for (Element ads : elements) {
if (ads.attr("title").equals(projectName)) {
ProjectsDTO projectsDTO = new ProjectsDTO();
projectsDTO.setTitle(ads.attr("title"));
projectsDTO.setHref(consts.url + "/tree/main/" + projectName);
Document connectionMidProject = Jsoup.connect(consts.url + "/tree/main/" + projectName).get();
Elements elementsMidProject = connectionMidProject.getElementsByTag("a");
for(Element adsNext : elementsMidProject){
if (adsNext.attr("title").equals("readme.md")){
Document connectionReadmeMd = Jsoup.connect(consts.url + "/blob/main/" + projectName + "/readme.md").get();
Elements elementsReadmeMd = connectionReadmeMd.select("div#readme");
for(Element adsReadme : elementsReadmeMd){
String desc = adsReadme.select("p").toString();
String descFront = desc.substring(14);
String descBack = descFront.substring(0, descFront.length()-4);
projectsDTO.setDescription(descBack);
}
}
}
Document connectionFordateCreated = Jsoup.connect(consts.url + "/commits/main/" + projectName).get();
Elements elementsForDateCreated = connectionFordateCreated.select("div.span10.offset1 h2");
projectsDTOS.add(projectsDTO);
}
}
} catch (IOException ex) {
logger.warning("The provided project name is invalid...Try again!");
ex.printStackTrace();
}
return projectsDTOS;
}
public static Set<ProjectsDTO> getAllProjects(){
Set<ProjectsDTO> projectsDTOS = new HashSet<>();
try{
Document document = Jsoup.connect(consts.url).get();
Elements elements = document.getElementsByTag("a");
for (Element ads : elements) {
if (ads.attr("title").equals("") || ads.attr("title").equals("GitHub")){
continue;
}
else{
ProjectsDTO projectsDTO = new ProjectsDTO();
projectsDTO.setTitle(ads.attr("title"));
projectsDTO.setHref(consts.url + "/");
projectsDTOS.add(projectsDTO);
}
}
}catch(IOException ex){
logger.warning("The provided project name is invalid...Try again!");
ex.printStackTrace();
}
return projectsDTOS;
}
}
This is website code:
<div class="TimelineItem-body">
<h2 class="f5 text-normal">Commits on Aug 9, 2021</h2>
Thank you all in advance :) Happy coding