java1234开源博客系统
博客信息

htmlunit 操作table表格(一)

发布时间:『 2017-04-13 10:49』  博客类别:htmlunit  阅读(5771) 评论(0)

htmlunit提供了对table表格的操作支持:


这里先演示下操作简单表格:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>简单表格</title>
</head>
<body>
	<table id="table1">
		<tr>
			<th>学号</th>
			<th>姓名</th>
		</tr>
		<tr>
			<td>007</td>
			<td>米歇尔</td>
		</tr>
	</table>
</body>
</html>


地址:http://www.java1234.com/crawler/table01.html


遍历所有行所有列代码:

package com.open1111;

import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;

public class HtmlUnitTest6 {

	public static void main(String[] args) {
		WebClient webClient=new WebClient(BrowserVersion.FIREFOX_52); // 实例化Web客户端	
		webClient.getOptions().setCssEnabled(false); // 取消css支持
		webClient.getOptions().setJavaScriptEnabled(false); // 取消javascript支持
		try {
			HtmlPage page=webClient.getPage("http://www.java1234.com/crawler/table01.html"); // 解析获取页面
			HtmlTable table=page.getHtmlElementById("table1");
			for(HtmlTableRow row:table.getRows()){ // 遍历所有行
				for(HtmlTableCell cell:row.getCells()){  // 遍历所有列
					System.out.print(cell.asText()+" ");
				}
				System.out.println();
			}
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			webClient.close(); // 关闭客户端,释放内存
		}
	}
}


运行输出:

学号 姓名

007 米歇尔


当然也提供了直接获取指定行指定列的api:

package com.open1111;

import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;

public class HtmlUnitTest6 {

	public static void main(String[] args) {
		WebClient webClient=new WebClient(BrowserVersion.FIREFOX_52); // 实例化Web客户端	
		webClient.getOptions().setCssEnabled(false); // 取消css支持
		webClient.getOptions().setJavaScriptEnabled(false); // 取消javascript支持
		try {
			HtmlPage page=webClient.getPage("http://www.java1234.com/crawler/table01.html"); // 解析获取页面
			HtmlTable table=page.getHtmlElementById("table1");
			System.out.println("获取第1行第2列:"+table.getCellAt(0, 1).asText());
			System.out.println("获取第2行第1列:"+table.getCellAt(1, 0).asText());
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			webClient.close(); // 关闭客户端,释放内存
		}
	}
}

运行输出:

获取第1行第2列:姓名

获取第2行第1列:007




关键字:   htmlunit     操作table  
关注Java1234微信公众号
博主信息
Java1234_小锋
(知识改变命运,技术改变世界)
Powered by Java1234 V3.0 Copyright © 2012-2016 Java知识分享网 版权所有