made the web scraping workm abd save the elements to a list :)
This commit is contained in:
23
src/main/java/nl/herpiederpiee/appie_scraper/BonusItem.java
Normal file
23
src/main/java/nl/herpiederpiee/appie_scraper/BonusItem.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package nl.herpiederpiee.appie_scraper;
|
||||
|
||||
import com.microsoft.playwright.ElementHandle;
|
||||
|
||||
public class BonusItem {
|
||||
String title;
|
||||
String bonusText;
|
||||
String category;
|
||||
String imageURL;
|
||||
|
||||
BonusItem(ElementHandle element) {
|
||||
this.title = element.getAttribute("title");
|
||||
|
||||
ElementHandle bonusElement = element.querySelector(".promotion-label-base_textContainer__DFx6D");
|
||||
this.bonusText = bonusElement.innerHTML().replaceAll("<[^>]*>", " ");
|
||||
|
||||
ElementHandle categoryContainer = element.evaluateHandle("el => el.closest('section')").asElement();
|
||||
this.category = categoryContainer.getAttribute("id");
|
||||
|
||||
|
||||
this.imageURL = element.querySelector(".promotion-card-image_img__Ca5n8").getAttribute("data-src");
|
||||
}
|
||||
}
|
||||
46
src/main/java/nl/herpiederpiee/appie_scraper/Main.java
Normal file
46
src/main/java/nl/herpiederpiee/appie_scraper/Main.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package nl.herpiederpiee.appie_scraper;
|
||||
import com.microsoft.playwright.*;
|
||||
import com.microsoft.playwright.options.WaitUntilState;
|
||||
import org.json.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
|
||||
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
|
||||
|
||||
BrowserContext context = browser.newContext();
|
||||
Page bonusPagina = context.newPage();
|
||||
|
||||
|
||||
bonusPagina.navigate("https://www.ah.nl/bonus");
|
||||
System.out.println("Dom Content Loaded!");
|
||||
|
||||
TimeUnit.SECONDS.sleep(5);
|
||||
ArrayList<BonusItem> bonusItems = new ArrayList<BonusItem>();
|
||||
int counter = 0;
|
||||
Locator bonusElements = bonusPagina.locator(".promotion-card_root__tQA3z");
|
||||
for (ElementHandle bonusElement : bonusElements.elementHandles()){
|
||||
BonusItem bonusItem = new BonusItem(bonusElement);
|
||||
bonusItems.add(bonusItem);
|
||||
counter++;
|
||||
}
|
||||
System.out.println("Amount of items: " + counter);
|
||||
// get random item from array
|
||||
Random random = new Random();
|
||||
BonusItem chosenItem = bonusItems.get(random.nextInt(bonusItems.size()));
|
||||
System.out.println("Random Item:\n"+chosenItem.title+" => "+chosenItem.bonusText + " ("+chosenItem.category+")\nImage URL:"+chosenItem.imageURL);
|
||||
|
||||
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user