日期:2014-05-16  浏览次数:21429 次

使用htmlUnit和Jsoup获取163邮箱通讯录

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.NameValuePair;

?

public class ContactList {
??? public static void oneSixThree() throws FailingHttpStatusCodeException, IOException {
??????? String loginUrl = "http://reg.163.com/login.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D3";
??
??????? WebClient client = new WebClient();
??????? client.getOptions().setJavaScriptEnabled(true);
??????? client.getOptions().setCssEnabled(false);
??????? client.getOptions().setThrowExceptionOnScriptError(false);
??????? client.getOptions().setTimeout(10000);

?

??????? List<NameValuePair> reqParams = new ArrayList<NameValuePair>();
??????? reqParams.add(new NameValuePair("product", "mail163"));
??????? reqParams.add(new NameValuePair("username", "xxx"));
??????? reqParams.add(new NameValuePair("password", "yyy"));
??????? reqParams.add(new NameValuePair("selType", "jy"));

?

??????? WebRequest webRequest = new WebRequest(new URL(loginUrl), HttpMethod.POST);
??????? webRequest.setRequestParameters(reqParams);??
??????? HtmlPage emailPage = (HtmlPage) client.getPage(webRequest);
??????? String url = emailPage.getUrl().toString();

??????? String sid = url.substring(url.indexOf("sid="));

??????? String contactUrl = "http://tg4a84.mail.163.com/jy3/address/addrprint.jsp?" + sid;
??????? HtmlPage contactPage = (HtmlPage) client.getPage(contactUrl);

?

??????? Document doc = Jsoup.parse(contactPage.asXml());
??????? Elements names = doc.select(".gTitleSub .mTT");
??????? Elements addresses = doc.select(".gTable tbody tr:first-child td");
??????? int size = names.size();
??????? for (int i = 0; i < size; i++) {
??????????? System.out.println(names.get(i).text() + " " + addresses.get(i).text());
??? }
?}

}