made the web scraping workm abd save the elements to a list :)

This commit is contained in:
Valentijn van der Jagt
2025-11-22 01:01:26 +01:00
parent fe75038ce0
commit ec2db75eb8
4 changed files with 75 additions and 10 deletions

View 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");
}
}

View 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);
}
}
}

View File

@@ -1,9 +0,0 @@
package org.example;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
}
}