日期:2014-05-17  浏览次数:20647 次

htmlunit分页爬去数据实现
public static void loginAndRechargeResult(String payNoValue,
String j_id_jsp_1155368641_9Value,
String j_id_jsp_1155368641_11Value,
String j_id_jsp_1155368641_13Value,
String filePath) {
String filePath1 = "";
if (filePath.equals("")) {
filePath1 = "F:\\FileVal3ue.txt";
} else {
filePath1 = filePath;
}
try {
WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
HtmlPage page1 = (HtmlPage) client
.getPage("http://XXXXX/vams/");
HtmlForm form = (HtmlForm) page1.getForms().get(0);
HtmlInput button = form.getInputByName("login:j_id_jsp_28762255_4");
HtmlTextInput username = (HtmlTextInput) form
.getInputByName("login:userName");
HtmlPasswordInput password = (HtmlPasswordInput) form
.getInputByName("login:password");
username.setValueAttribute("用户名");
password.setValueAttribute("密码");
HtmlPage page2 = (HtmlPage) button.click();
System.err
.println("************************充值查询结果集**********************************");
client.setJavaScriptEnabled(true);
String Recharge = "";
Recharge = "http://xxxxx/vams/WebPay/WebPaySearch.faces?showType=2";
HtmlPage Recharge1 = (HtmlPage) client.getPage(Recharge);
HtmlForm rechargeForm = (HtmlForm) Recharge1.getForms().get(0);
// 充值号码
HtmlTextInput payNo = (HtmlTextInput) rechargeForm
.getInputByName("form1:payNo");
payNo.setValueAttribute(payNoValue);
// 充值时间:
HtmlTextInput j_id_jsp_1155368641_9 = (HtmlTextInput) rechargeForm
.getInputByName("form1:j_id_jsp_1155368641_9");
j_id_jsp_1155368641_9.setValueAttribute(j_id_jsp_1155368641_9Value);
HtmlTextInput j_id_jsp_1155368641_11 = (HtmlTextInput) rechargeForm
.getInputByName("form1:j_id_jsp_1155368641_11");
j_id_jsp_1155368641_11
.setValueAttribute(j_id_jsp_1155368641_11Value);
// 充值金额
HtmlTextInput j_id_jsp_1155368641_13 = (HtmlTextInput) rechargeForm
.getInputByName("form1:j_id_jsp_1155368641_13");
j_id_jsp_1155368641_13
.setValueAttribute(j_id_jsp_1155368641_13Value);
// button
HtmlSubmitInput button1 = (HtmlSubmitInput) rechargeForm
.getInputByName("form1:j_id_jsp_1155368641_32");
HtmlPage page12 = (HtmlPage) button1.click();
System.out.println(page12.asXml());
File desFile = new File(filePath1);// 目标文件
HtmlForm aa = page12.getForms().get(0);
HtmlDivision div = (HtmlDivision) aa
.getByXPath("//div[@id='zhu1']").get(1);
String aad = div.asText();
int ddd = (Integer.parseInt(aad.split("共")[2].split("页")[0].trim()));
FileWriter fw = new FileWriter(desFile);
fw.append("代理商编号     充值时间       充值号码  充值金额  充值类型     服务类型");
fw.append("\r\n");
int dd = 0;
for (int i = 0; i < ddd; i++) {
if (i == 0) {
DomNodeList<HtmlElement> tables = page12
.getElementsByTagName("table");
final HtmlTable table = (HtmlTable) tables.get(tables
.size() - 1);
for (final HtmlTableRow row : table.getBodies().get(0)
.getRows()) {
String alertPring = "";
for (final HtmlTableCell cell : row.getCells()) {
alertPring += cell.asText() + "  ";
}
fw.append(alertPring);
fw.append("\r\n");
}
} else {
HtmlPage ahtmpr = div.getHtmlElementsByTagName("a").get(3)
.click();
System.out.println(ahtmpr.asXml());
DomNodeList<HtmlElement> tables2 = ahtmpr
.getElementsByTagName("table");
final HtmlTable table = (HtmlTable) table