在学习使用httpclient抽取网页代码,可是抽取下来发现,为什么原网页上翻页的地方没有对应代码,如下:
原网页:抽取出的代码部分:使用浏览器审查元素,对应的代码:第一次使用httpclient,用java抽取网页。Java浏览器httpclient

解决方案 »

  1.   

    抽取网页的源码:public static boolean downloadPage (String path) throws HTTPException, IOException {
    System.out.println("RetrivePage.downloadPage start");
    InputStream input = null;
    OutputStream output = null;
    //得到post方法
    // PostMethod postMethod = new PostMethod();
    // //设置post方法的参数
    // NameValuePair[] postData = new NameValuePair[2];
    // postData[0] = new NameValuePair("name", "lietu");
    // postData[1] = new NameValuePair("password", "*****");
    // postMethod.addParameters(postData);
    GetMethod getMethod = new GetMethod(path);
    //执行,返回状态码
    // int statusCode = httpClient.executeMethod(postMethod);
    int statusCode = httpClient.executeMethod(getMethod);
    System.out.println("statusCode:"+statusCode);
    //针对状态码进行处理
    if (statusCode == HttpStatus.SC_OK) {
    // input = postMethod.getResponseBodyAsStream();
    input = getMethod.getResponseBodyAsStream();
    //得到文件名
    // String filename = path.substring(path.lastIndexOf('/')+1);
    String filename = "test.txt";
    //获得文件输出流
    output = new FileOutputStream(filename);

    //输出到文件
    int tempByte = -1;
    while ((tempByte = input.read()) > 0) {
    output.write(tempByte);
    }
    //关闭输入输出流
    if (input!=null) {
    input.close();
    }
    if (output!=null) {
    output.close();
    }
    System.out.println("RetrivePage.downloadPage end");
    return true;
    }
    return false;
    }