简体   繁体   English

如何使用JSOUP解析html表?

[英]How to parse html table with JSOUP?

need some help with parsing table from html with JSOUP. 需要一些使用JSOUP从html解析表的帮助。

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? 为什么jsoup不想解析任何想法? (have also tried to get element by id like "search-result") (还尝试通过ID来获取元素,例如“搜索结果”)

Thanks in advance. 提前致谢。

this is happened because this url is not a static page. 发生这种情况是因为此url不是静态页面。 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. 如果要获取此页面的html,则应首先使用http客户端库获取解析该页面的内容。

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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM