congyiwei 2015-04-14 04:37 采纳率: 100%
浏览 2616
已采纳

求助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%24cphMainFrame%24ddlTimePoint=28&__EVENTTARGET=ctl00%24cphMainFrame%24aspnetpager1&__LASTFOCUS=&__VIEWSTATE=%2FwEPDwULLTEwNTcyNDc4NjkPZBYCZg9kFgICAQ9kFgQCAQ8WAh4LXyFJdGVtQ291bnQCCxYWZg9kFgJmDxUCAzEwMQzlhpzlia%2Fkuqflk4FkAgEPZBYCZg8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vATX2wSsTwyj9sMOqdXBRc0&__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");

    }

}
  • 写回答

3条回答 默认 最新

  • 毕小宝 博客专家认证 2015-04-14 06:42
    关注

    代码没有问题,可能网站数据是实时变动的,。
    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%2BWTgWQCBg9kFgJmDxUCAzEwMhwyMDEz5bm05Lul5YmN5bel5Lia5raI6LS55ZOBZAIHD2QWAmYPFQIDMTAzHzIwMTPlubTku6XliY3lt6XkuJrnlJ%2FkuqfotYTmlplkAggPZBYCZg8VAgMxMDQZMjAxM%2BW5tOS7peWJjeacjeWKoeS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QyMDExBDIwMTIEMjAxMwQyMDE0BDIwMTUUKwMQZ2dnZ2dnZ2dnZ2dnZ2dnZxYBAg9kAgkPEGRkFgECAWQCCw8QDxYCHwVnZBAVBAnor7fpgInmi6kCMDUCMTUCMjUVBAEwAjI4AjI5AjMwFCsDBGdnZ2dkZAIPDxYCHgdWaXNpYmxlZxYCZg9kFgJmD2QWAgIDD2QWAmYPZBYCAgMPFgIfBmcWAmYPZBYCZg9kFgICAQ8PFgYeCFBhZ2VTaXplAhQeEEN1cnJlbnRQYWdlSW5kZXgCAh4LUmVjb3JkY291bnQCL2RkZMVUAu8HLwRzj1xEKpBi8MSr0fYD&__EVENTVALIDATION=%2FwEWJAKS79jeCgK44pTJBQKQzNnOBQKQzK3lAgKQzLGYCwKQzIU%2FApDM6dMIApDM%2FfYBApDMwa0OApDM1cAGApDM%2BSkCkMzNzAgC%2B%2FX%2F1Q8C%2B%2FXDiAQC%2B%2FXXrw0C%2B%2FW7wgUC%2B%2FWP%2BQIC%2B%2FWTnAsC1pveuw4CxvS41gICxvS81gICxvSA1gICxvSE1gICxvSI1gICxvSM1gICxvSQ1gICxvTU1QICxvTY1QIC2fS01gIC2fS41gIC2fS81gIC0sfPsw4CzMfvsw4CzMfjsw4Cz8ePsA4C8vma7whOBwA2O0BJTn5kLqZv1C98W2UbZQ%3D%3D&__ASYNCPOST=true&&__EVENTARGUMENT=" + pageNo;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 fluent模拟静态气体扩散
  • ¥15 java根据模板,生成word文档,需要带目录
  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化