SRL Archive - General

[Java] Best way to parse the HTML from this page?


J J

2013-04-18 17:06:19

Looking to scrape the info from this page

http://www.autoweek.nl/async_nc/get_kenteken_page.php?page=1&cache=no&kenteken=13zpl2

So far I've been using a regex that works pretty well.
private static final Pattern p = Pattern.compile("class=\"naam\"(.*?)>(([a-zA-Z0-9 _.()/-]*[a-zA-Z0-9 _.()/-]))<(.*?)class=\"waarde\".*?>((.*?))<");
Atleast that works.

But there must be better and more viable ways to parse this.
There is a class with the name "naam" and a certain value, and a class with the name "waarde" with a certain value. Looking to get those values so I can use them in an Android App I'm making (Preview: http://puu.sh/2CBiK.png)

Some options are XPaths and SAX or some other parsers but I have no idea what the best is in this case.

Greetz,

JJ

J J

2013-04-21 09:41:34

I have been using JSoup. It's incredibly easy to use and I've managed to parse the hardest page now ;)
Looks like this
public ArrayList<String> parseWaardes(){
ArrayList<String> waardeLijst = new ArrayList<String>();

try {
Document doc = Jsoup.connect(link).data("query", "Java").userAgent("Mozilla").cookie("auth", "token") .timeout(3000).post();

Elements namen = doc.getElementsByClass("waarde");
Iterator<Element> it = namen.iterator();
while(it.hasNext()){
Element naam = it.next();
System.out.println("\nElement: " + naam);

String html = naam.html();
if(html.contains("icon_empty")){
waardeLijst.add("Nee");
continue;
}else if(html.contains("icon_full")){
waardeLijst.add("Ja");
continue;
}

String text = naam.text();
System.out.println("Element tekst: " + text);
waardeLijst.add(text);
}

System.out.println("\nDocument tekst:\n" + doc.text());
} catch (IOException e) {
e.printStackTrace();
}

return waardeLijst;
}

I suggest using it if you need to parse HTML :)