这个技术难题这两天一直攻不下来,代码过程即先登录人人主页 然后打印出个人主页源代码,然后到自己指定的一个好友页面,利用htmlparser包对该页面html文本爬取下来;但是实际效果是能打印出个人主页源代码,但一执行到进入指定好友页面,并爬取该页面,就爬不下来,爬下来的只是人人登录时(未登录)的人人首页。在网上查到要先保存cookie,不知道是不是这里出了问题。小弟已经焦头烂额~~public class Visit {
private static String userName = "[email protected]";
private static String password = "123456";   
  private static String redirectURL = "http://www.renren.com/home";//没有用 使用getFirstHeader()从服务器取的
  // Don't change the following URL   
  private static String renRenLoginURL = "http://www.renren.com/PLogin.do";   
   
  // The HttpClient is used in one session   
  private HttpResponse response;//用途
  public DefaultHttpClient httpclient = new DefaultHttpClient();//一直使用的客户端
// public HttpClient httpclient = new HttpClient();  
  private boolean login() {   
  HttpPost httpost = new HttpPost(renRenLoginURL);   
  // All the parameters post to the web site   
  List<NameValuePair> nvps = new ArrayList<NameValuePair>();   
  nvps.add(new BasicNameValuePair("origURL", redirectURL));   
  nvps.add(new BasicNameValuePair("domain", "renren.com"));   
  nvps.add(new BasicNameValuePair("isplogin", "true"));   
  nvps.add(new BasicNameValuePair("formName", ""));   
  nvps.add(new BasicNameValuePair("method", ""));   
  nvps.add(new BasicNameValuePair("submit", "登录"));   
    
  nvps.add(new BasicNameValuePair("email", userName));   
  nvps.add(new BasicNameValuePair("password", password));   
  try {   
  httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));   
  response = httpclient.execute(httpost);//执行登录请求 返回信息
  } catch (Exception e) {   
  e.printStackTrace();   
  return false;   
  } finally {   
  httpost.abort();//不废除会报错
  }   
  return true;   
  }
public String getRedirectLocation() {   
blablabla~~略去中间代码   
}
public String getText(String redirectLocation) {   
blablabla~~略去中间代码
}
public void printText() {   
if (login()) {   
String redirectLocation = getRedirectLocation();   
if (redirectLocation != null) {   
System.out.println(getText(redirectLocation));   
}
  
}   
}
private static String ENCODE = "gb2312";
private static void message(String szMsg) {
try{
System.out.println(new String(szMsg.getBytes(ENCODE), System.getProperty("file.encoding")));
}
catch(Exception e ){}
}
public void parse(int id){
Parser parser = new Parser("http://www.renren.com/" + id);for (NodeIterator i = parser.elements(); i.hasMoreNodes(); ) {
Node node = i.nextNode();
message("getText:"+node.getText());
  message("getPlainText:"+node.toPlainTextString());
message("toHtml:"+node.toHtml());
message("toHtml(true):"+node.toHtml(true));
message("toHtml(false):"+node.toHtml(false));
message("toString:"+node.toString());
message("=================================================");
}   
}
catch( Exception e ) {   
System.out.println( "Exception:"+e );
}
}
public static void main(String[] args) throws InterruptedException {   
Visit lw = new Visit();   
lw.printText();//登录并打印个人主页的源码(可以执行成功)
lw.parse(987654321);//到指定好友页面,爬取
}
}

解决方案 »

  1.   

    我又看了下,发现我之前有些误会了你的代码,你用Parser去抓取另一个页面是不行的,Parser不知道HttpClient的情况。要继续用HttpClient才行,也就是要:new HttpGet() ,然后继续 httpclient.execute() 跟你的登录过程的做法一致。
      

  2.   

    还想问一下,那怎么才能用htmlparser这个包呢
      

  3.   

    确实Parser根本不清楚HttpClient的情况,我只要继续用httpclient就能访问二级页面,但是怎么能让httpclient.execute()传回来的页面源码直接传给Parser,并让Parser包去爬取信息呢(parser类初始化好像只接受URL)
      

  4.   

    确实HTMLparser不清楚httpclient的情况,我只要继续用httpclient就可以访问其二级页面,但是怎么能让httpclient.execute()将传回来的二级页面的源码传给Parser,并让parser去爬取信息呢(parser类初始化好像只接受URL)
      

  5.   

    用这个函数就行了:parse(Reader in)但是要把HttpClient的返回信息包装为Reader。