get scores by product_id including pagination
This commit is contained in:
parent
be14bf06c3
commit
6eed415262
103
src/Main.java
103
src/Main.java
@ -4,9 +4,13 @@ import org.jsoup.nodes.Document;
|
|||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
import org.jsoup.select.Elements;
|
import org.jsoup.select.Elements;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.sql.SQLOutput;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
@ -19,19 +23,39 @@ public class Main {
|
|||||||
|
|
||||||
getScoresFromPage(doc);
|
getScoresFromPage(doc);
|
||||||
|
|
||||||
|
if (doc.selectFirst("div.pagination") != null && doc.selectFirst("div.pagination").selectFirst("li.arrow-next") != null) {
|
||||||
System.out.println("\n\n");
|
getScoresFromNextPage(Jsoup.connect("https://www.ceneo.pl/" + doc.selectFirst("div.pagination").selectFirst("li.arrow-next").selectFirst("a").attr("href")).get());
|
||||||
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Object getTextFromElement(Element element, String cssQuery) {
|
private static void getScoresFromNextPage(Document document) {
|
||||||
|
getScoresFromPage(document);
|
||||||
|
|
||||||
|
if (document.selectFirst("div.pagination") != null && document.selectFirst("div.pagination").selectFirst("li.arrow-next") != null) {
|
||||||
try {
|
try {
|
||||||
|
getScoresFromNextPage(Jsoup.connect("https://www.ceneo.pl/" + document.selectFirst("div.pagination").selectFirst("li.arrow-next").selectFirst("a").attr("href")).get());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getTextFromElement(Element element, String cssQuery) {
|
||||||
|
if (element.selectFirst(cssQuery) != null) {
|
||||||
return element.selectFirst(cssQuery).text();
|
return element.selectFirst(cssQuery).text();
|
||||||
} catch (NullPointerException e) {
|
}
|
||||||
return 0;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getSizeOfElementsFromElement(Element element, String cssQuery) {
|
||||||
|
if (element.selectFirst(cssQuery) != null) {
|
||||||
|
return String.valueOf(element.select(cssQuery).select("li").size());
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,47 +63,50 @@ public class Main {
|
|||||||
|
|
||||||
Elements products = document.select("li.review-box");
|
Elements products = document.select("li.review-box");
|
||||||
for (Element product : products) {
|
for (Element product : products) {
|
||||||
System.out.println(getTextFromElement(product, "span.review-score-count"));
|
scoreList.add(new Score(
|
||||||
// System.out.println(product);
|
getTextFromElement(product, "span.review-score-count"),
|
||||||
// Element score = product.selectFirst("span.review-score-count");
|
getTextFromElement(product, "p.product-review-body"),
|
||||||
// try {
|
getSizeOfElementsFromElement(product, "div.pros-cell"),
|
||||||
// System.out.println("score: " + score.text());
|
getSizeOfElementsFromElement(product, "div.cons-cell")
|
||||||
// } catch (NullPointerException e) {
|
));
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Element description = product.selectFirst("p.product-review-body");
|
|
||||||
// try {
|
|
||||||
// System.out.println("description: \n" + description.text());
|
|
||||||
// } catch (NullPointerException e) {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// Element advElement = product.selectFirst("div.pros-cell");
|
|
||||||
// Elements advCounter = advElement.select("li");
|
|
||||||
// System.out.println("adv: " + advCounter.size());
|
|
||||||
// } catch (NullPointerException e) {
|
|
||||||
// System.out.println("adv: 0");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// Element dissadvElement = product.selectFirst("div.cons-cell");
|
|
||||||
// Elements dissadvCounter = dissadvElement.select("li");
|
|
||||||
// System.out.println("adv: " + dissadvCounter.size());
|
|
||||||
// } catch (NullPointerException e) {
|
|
||||||
// System.out.println("dissadv: 0");
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void removeObjectsContainedNull() {
|
||||||
|
scoreList = scoreList.stream().filter( x ->
|
||||||
|
x.getScore() != null &&
|
||||||
|
x.getDescription() != null &&
|
||||||
|
x.getAdvantagesCounter() != null &&
|
||||||
|
x.getDissadvantagesCounter() != null)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void saveListToFile() {
|
||||||
|
FileWriter fileWriter = null;
|
||||||
|
try {
|
||||||
|
fileWriter = new FileWriter("scores.txt");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintWriter printWriter = new PrintWriter(fileWriter);
|
||||||
|
for(Score score: scoreList) {
|
||||||
|
printWriter.printf(score.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
printWriter.close();
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
int[] productList = new int[]{29362313, 44279952};
|
int[] productList = new int[]{70021762, 26740026};
|
||||||
for (Integer item : productList) {
|
for (Integer item : productList) {
|
||||||
getScoresByItemId(item);
|
getScoresByItemId(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeObjectsContainedNull();
|
||||||
|
System.out.println(scoreList.size());
|
||||||
|
saveListToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
package entities;
|
package entities;
|
||||||
|
|
||||||
public class Score {
|
public class Score {
|
||||||
private float score;
|
private String score;
|
||||||
private String description;
|
private String description;
|
||||||
private int advantagesCounter;
|
private String advantagesCounter;
|
||||||
private int dissadvantagesCounter;
|
private String dissadvantagesCounter;
|
||||||
|
|
||||||
public float getScore() {
|
public String getScore() {
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScore(float score) {
|
public void setScore(String score) {
|
||||||
this.score = score;
|
this.score = score;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,19 +22,34 @@ public class Score {
|
|||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAdvantagesCounter() {
|
public String getAdvantagesCounter() {
|
||||||
return advantagesCounter;
|
return advantagesCounter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAdvantagesCounter(int advantagesCounter) {
|
public void setAdvantagesCounter(String advantagesCounter) {
|
||||||
this.advantagesCounter = advantagesCounter;
|
this.advantagesCounter = advantagesCounter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDissadvantagesCounter() {
|
public String getDissadvantagesCounter() {
|
||||||
return dissadvantagesCounter;
|
return dissadvantagesCounter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDissadvantagesCounter(int dissadvantagesCounter) {
|
public void setDissadvantagesCounter(String dissadvantagesCounter) {
|
||||||
this.dissadvantagesCounter = dissadvantagesCounter;
|
this.dissadvantagesCounter = dissadvantagesCounter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Score(String score, String description, String advantagesCounter, String dissadvantagesCounter) {
|
||||||
|
this.score = score;
|
||||||
|
this.description = description;
|
||||||
|
this.advantagesCounter = advantagesCounter;
|
||||||
|
this.dissadvantagesCounter = dissadvantagesCounter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return score +
|
||||||
|
";;;;;" + description + '\'' +
|
||||||
|
";;;;;" + advantagesCounter +
|
||||||
|
";;;;;" + dissadvantagesCounter + "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user