只能重新开贴了,望各位大牛能帮帮小弟~
这个技术难题这两天一直攻不下来,代码过程即先登录人人主页 然后打印出个人主页源代码,然后到自己指定的一个好友页面,利用htmlparser包对该页面html文本爬取下来;但是实际效果是能打印出个人主页源代码,但一执行到进入指定好友页面,并爬取该页面,就爬不下来,爬下来的只是人人登录时(未登录)的人人首页。在网上查到要先保存cookie,不知道是不是这里出了问题。小弟已经焦头烂额~~
public class Visit {
private static String userName = "123456678@123.com";
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 InterruptedExceptio