-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawler.java
More file actions
56 lines (47 loc) · 1.93 KB
/
Copy pathCrawler.java
File metadata and controls
56 lines (47 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Crawler {
private static Logger LOG = LoggerFactory.getLogger(Crawler.class.getName());
private static String dvlaUrl = "https://vehicleenquiry.service.gov.uk/ConfirmVehicle?Vrm=";
WebDriver driver;
public Crawler() {
System.setProperty("phantomjs.binary.path", "libs/phantomjs");
driver = new PhantomJSDriver();
}
public void startCrawl() {
try {
String registration = "HUE166";
driver.get(dvlaUrl + registration);
Document document = Jsoup.parse(driver.getPageSource());
Element results = document.selectFirst(".list-summary");
// note that sometimes the DVLA website seems to return no results even when the vehicle exists!
if (results != null) {
Elements dataItems = results.select(".list-summary-item");
// need to parse into Elements, but there is only one make
Elements make = dataItems.select(":contains(Make)").select("strong");
LOG.debug("Make: '" + make.text() + "'");
// need to parse into Elements, but there is only one colour
Elements colour = dataItems.select(":contains(Colour)").select("strong");
LOG.debug("Colour: '" + colour.text() + "'");
} else {
LOG.debug("No results for registration " + registration);
}
}
catch(Exception ex) {
// ignore, just log
LOG.error(ex.getMessage() + ex.getStackTrace());
}
finally {
if (driver != null) {
driver.quit();
}
}
}
}