Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand Article 1 examples #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
target
target/*
*.pdf
*.DS_STORE
*.DS_STORE

.idea
78 changes: 46 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>
<groupId>fr.ksahin</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Blog</name>


<dependencies>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<groupId>fr.ksahin</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Blog</name>

<dependency>
<groupId>com.github.detro</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>23</release>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.70.0</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.0</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>

<dependency>
<groupId>com.github.detro</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>
30 changes: 0 additions & 30 deletions src/main/java/blog/article1/Item.java

This file was deleted.

56 changes: 0 additions & 56 deletions src/main/java/blog/article1/WebScraper.java

This file was deleted.

39 changes: 39 additions & 0 deletions src/main/java/blog/article1/e1_plain/E1_Plain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package blog.article1.e1_plain;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

class E1_Plain {

public static void main(String[] args) throws IOException {
var searchQuery = "iphone 13";
var searchUrl = "https://newyork.craigslist.org/search/moa?query=%s".formatted(URLEncoder.encode(searchQuery, StandardCharsets.UTF_8));

System.out.println("searchUrl = " + searchUrl);

try (var client = new WebClient()) {
client.getOptions().setCssEnabled(false);
client.getOptions().setJavaScriptEnabled(false);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);

HtmlPage page = client.getPage(searchUrl);
for (var htmlItem : page.<HtmlElement>getByXPath("//li[contains(@class,'cl-static-search-result')]")) {
HtmlAnchor itemAnchor = htmlItem.getFirstByXPath(".//a");
HtmlElement itemTitle = htmlItem.getFirstByXPath(".//div[@class='title']");
HtmlElement itemPrice = htmlItem.getFirstByXPath(".//div[@class='price']");
HtmlElement itemLocation = htmlItem.getFirstByXPath(".//div[@class='location']");

if (itemAnchor != null && itemTitle != null) {
System.out.printf("Name: %s, Price: %s, Location: %s, URL: %s%n", itemTitle.asNormalizedText(), itemPrice.asNormalizedText(), (itemLocation == null) ? "N/A" : itemLocation.asNormalizedText(), itemAnchor.getHrefAttribute());
}
}
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/blog/article1/e2_json/E2_ResultsAsJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package blog.article1.e2_json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

class E2_ResultsAsJson {

private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public static void main(String[] args) throws IOException {
var searchQuery = "iphone 13";
var searchUrl = "https://newyork.craigslist.org/search/moa?query=%s".formatted(URLEncoder.encode(searchQuery, StandardCharsets.UTF_8));

System.out.println("searchUrl = " + searchUrl);

try (var client = new WebClient()) {
client.getOptions().setCssEnabled(false);
client.getOptions().setJavaScriptEnabled(false);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);

HtmlPage page = client.getPage(searchUrl);
for (var htmlItem : page.<HtmlElement>getByXPath("//li[contains(@class,'cl-static-search-result')]")) {
HtmlAnchor itemAnchor = htmlItem.getFirstByXPath(".//a");
HtmlElement itemTitle = htmlItem.getFirstByXPath(".//div[@class='title']");
HtmlElement itemPrice = htmlItem.getFirstByXPath(".//div[@class='price']");
HtmlElement itemLocation = htmlItem.getFirstByXPath(".//div[@class='location']");

if (itemAnchor != null && itemTitle != null) {
var itemName = itemTitle.asNormalizedText();
var itemUrl = itemAnchor.getHrefAttribute();
var itemPriceText = itemPrice.asNormalizedText();
var itemLocationText = (itemLocation == null) ? "N/A" : itemLocation.asNormalizedText();

var item = new Item(itemName, new BigDecimal(itemPriceText.replace("$", "").replace(",", ".")), itemLocationText, itemUrl);
System.out.println("item = " + OBJECT_MAPPER.writeValueAsString(item));
}
}
}
}

record Item(String title, BigDecimal price, String location, String url) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package blog.article1.e3_multiple_cities;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

class E3_MultipleCities {

private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public static void main(String[] args) throws IOException {
var searchQuery = "iphone 13";
var cities = List.of("newyork", "boston", "washingtondc");

try (var client = new WebClient()) {
client.getOptions().setCssEnabled(false);
client.getOptions().setJavaScriptEnabled(false);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);

for (String city : cities) {
var searchUrl = "https://%s.craigslist.org/search/moa?query=%s".formatted(city, URLEncoder.encode(searchQuery, StandardCharsets.UTF_8));

System.out.println("searchUrl = " + searchUrl);

HtmlPage page = client.getPage(searchUrl);
for (var htmlItem : page.<HtmlElement>getByXPath("//li[contains(@class,'cl-static-search-result')]")) {
HtmlAnchor itemAnchor = htmlItem.getFirstByXPath(".//a");
HtmlElement itemTitle = htmlItem.getFirstByXPath(".//div[@class='title']");
HtmlElement itemPrice = htmlItem.getFirstByXPath(".//div[@class='price']");
HtmlElement itemLocation = htmlItem.getFirstByXPath(".//div[@class='location']");

if (itemAnchor != null && itemTitle != null) {
var itemName = itemTitle.asNormalizedText();
var itemUrl = itemAnchor.getHrefAttribute();
var itemPriceText = itemPrice.asNormalizedText();
var itemLocationText = (itemLocation == null) ? "N/A" : itemLocation.asNormalizedText();

var item = new Item(itemName, new BigDecimal(itemPriceText.replace("$", "").replace(",", ".")), itemLocationText, itemUrl);
System.out.println("item = " + OBJECT_MAPPER.writeValueAsString(item));
}
}
}
}
}

record Item(String title, BigDecimal price, String location, String url) {
}
}
Loading