程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-求助jsoup取頁面之後輸出的問題

java-求助jsoup取頁面之後輸出的問題

編輯:編程綜合問答
求助jsoup取頁面之後輸出的問題

取出的表格數據為空,請問這是string body的問題麼...
如果要解決問題應該怎樣修改...

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class Main {

    public static void main(String[] args) throws Throwable {

        for (int i = 1; i <= 3; i++) {
            System.out.println(getPrice(i));
        }

    }

    static List<String> getPrice(int pageNo) throws Throwable {

        Document doc = Jsoup.parse(getText(pageNo));

        Elements trs = doc.select("#ctl00_cphMainFrame_Table1 tr");

        List<String> result = new ArrayList<String>(trs.size());

        for (int i = 1, l = trs.size(); i < l; i++) {
            Element tr = trs.get(i);

            result.add(tr.child(5).text());
        }

        return result;

    }

    static String getText(int pageNo) throws Throwable {

        URL url = new URL("http://www.lnprice.gov.cn/wjjc/jgjc/ReportByDateOfPivot.aspx?PriceBureauMainType_Id=101&YM=201502&DP=28");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("POST");

        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");

        conn.setDoOutput(true);

        conn.connect();

        String body = "ctl00%24cphMainFrame%24ScriptManager1=ctl00%24cphMainFrame%24UpdatePanel1%7Cctl00%24cphMainFrame%24aspnetpager1&ctl00%24cphMainFrame%24ddlYear=2015&ctl00%24cphMainFrame%24ddlMonth=02&ctl00%24cphMain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AL79devDQL79bvCBQL79Y%2F5AgL79ZOcCwLWm967DgLG9LjWAgLG9LzWAgLG9IDWAgLG9ITWAgLG9IjWAgLG9IzWAgLG9JDWAgLG9NTVAgLG9NjVAgLZ9LTWAgLZ9LjWAgLZ9LzWAgLSx8%2BzDgLMx%2B%2BzDgLMx%2BOzDgLPx4%2BwDgLy%2BZrvCEFw0vATX2wSsTwyj9sMOqdXBRc0&__ASYNCPOST=true&__EVENTARGUMENT=" + pageNo;

        conn.getOutputStream().write(body.getBytes());

        byte[] buff = new byte[4096];
        int count;

        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
        InputStream in = conn.getInputStream();

        while((count = in.read(buff)) != -1) {
            out.write(buff, 0, count);
        }

        conn.disconnect();

        return out.toString("UTF-8");

    }

}

最佳回答:


代碼沒有問題,可能網站數據是實時變動的,。
body內容試試這個參數,這個是剛剛抓到的(如果不行,找個最新的再試試):

 String body = "ctl00%24cphMainFrame%24ScriptManager1=ctl00%24cphMainFrame%24UpdatePanel1%7Cctl00%24cphMainFrame%24aspnetpager1&ctl00%24cphMainFrame%24ddlYear=2015&ctl00%24cphMainFrame%24ddlMonth=02&ctl00%24cphMainFrame%24ddlTimePoint=28&__EVENTTARGET=ctl00%24cphMainFrame%24aspnetpager1&__LASTFOCUS=&__VIEWSTATE=%2FwEPDwULLTEwNTcyNDc4NjkPZBYCZg9kFgICAQ9kFgQCAQ8WAh4LXyFJdGVtQ291bnQCCxYWZg9kFgJmDxUCAzEwMQzlhpzlia%2Fkuqflk4FkAgEPZBYCZg8VAgMxMDcP5bel5Lia5raI6LS55ZOBZAICD2QWAmYPFQIDMTA4EuW3peS4mueUn%2BS6p%2Bi1hOaWmWQCAw9kFgJmDxUCAzEwORjln47luILlsYXmsJHmnI3liqHku7fmoLxkAgQPZBYCZg8VAgMxMTAY5Yac5p2R5bGF5rCR5pyN5Yqh5Lu35qC8ZAIFD2QWAmYPFQIDMTExDOa2ieWGnOS6p%2BWTgWQCBg9kFgJmDxUC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