made the web scraping workm abd save the elements to a list :)
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -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>
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user