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

@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId> <groupId>nl.herpiederpiee.appie_scraper</groupId>
<artifactId>AppieBonusScraper</artifactId> <artifactId>AppieBonusScraper</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
@@ -17,6 +17,11 @@
<artifactId>playwright</artifactId> <artifactId>playwright</artifactId>
<version>1.55.0</version> <version>1.55.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20250517</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

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) {
}
}