need some help with parsing table from html with JSOUP.
So here is the link: long link with search-result
I need to extract data from table in search-result section. Currently i have something like this:
package com.company;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new File("MonitorUL.csv"), "windows-1251");
final String colNames = "DepositName;Percentage;MinAmount;Duration";
StringBuilder builder = new StringBuilder();
builder.append(colNames + "\n");
String url = "http://www.banki.ru/products/corporate/search/sankt-peterburg/?CURRENCY=982&AMOUNT=&PERIOD=985&show=all&curcount=all&bankid%5B0%5D=322&bankid%5B1%5D=76620&bankid%5B2%5D=327&bankid%5B3%5D=4389&bankid%5B4%5D=2764&bankid%5B5%5D=960&bankid%5B6%5D=325&bankid%5B7%5D=690&bankid%5B8%5D=5306&bankid%5B9%5D=4725&bankid%5B10%5D=193284&bankid%5B11%5D=68665&bankid%5B12%5D=5919&bankid%5B13%5D=191203&bankid%5B14%5D=68768&bankid%5B15%5D=4045#search-result";
Document doc = Jsoup.parse(url);
System.out.println(doc.toString());
Element table = doc.getElementById("thead");
Elements rows = table.select("tr");
for (int i = 0; i < rows.size() ; i++) {
Element row = rows.get(i);
Elements cols = row.select("td");
for (int j = 0; j < cols.size(); j++) {
builder.append(cols.get(j).text());
builder.append(";");
}
builder.append("\n");
}
pw.write(builder.toString());
pw.close();
}
}
But it does not work. Any ideas why jsoup do not want to parse? (have also tried to get element by id like "search-result")
Thanks in advance.
this is happened because this url is not a static page. if you want have the html of this page you should first use a http client lib to get the content of the page the parse it.
Following code snippet can be helpful for you:
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setTimeout(10000);
try {
HtmlPage htmlPage = webClient.getPage(url);
Document doc = Jsoup.parse(htmlPage.asXml());
Elements table = doc.getElementsByAttributeValueMatching("id","search-result");// This will select the entire section of the table with the "id"
Elements rows = table.select("tr");
System.out.println("No of rows in the table : "+ rows.size());
for (int i = 0; i < rows.size() ; i++) {
Element row = rows.get(i);
Elements cols = row.select("td");
for (int j = 0; j < cols.size(); j++) {
System.out.println(cols.get(j).text()); //modified this lines just to print the result on the console. You can modify this accordingly.
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
webClient.close();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.