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

使用JSoup生成本地HTML
实现功能:
1.根据URL抓取网页
2.添加(修改)网页内容
3.下载与网页相关的js,css,img,flash,iframe等
4.修改js,css,img,flash,iframe的路径
5.添加注释<!-- saved from url=("+new DecimalFormat("0000").format(url.length())+")"+url+" -->,网页不会弹出限制运行脚本提示
6.按照编码方式生成本地页面

工具类:

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CreateHtmlJsoup {
	public static void main(String[] args) {
		makeHeatMapHtml();
	}

	/**
	 * 生成HTML到本地的临时目录中
	 */
	public static void makeHeatMapHtml() {
		System.out.println("下载页面开始......");
		long start = System.currentTimeMillis();
		String url = "";//
		String filepath = "";// 
		String filename = "";// 
		String ecoding = "";// 
		
		url = "http://finance.people.com.cn/GB/index.html";
		filepath = "F:\\people.temp\\";
		filename = "index.html";// 文件名称
		ecoding = "gbk";// 编码格式
		if (copyURLToHtml(url, filepath, filename, ecoding)) {// 如果页面访问成功,下载URL网页到本地临时目录
			copyDirToDir(filepath, StringUtils.replace(filepath, ".temp",
					""));// 把临时目录中的文件复制到本地
			delTempFile(filepath);// 删除临时目录
		} 

		System.out.println("下载页面完成 ,用时"
				+ (System.currentTimeMillis() - start) / 1000 / 60 + "分钟");
	}

	/**
	 * 文件夹copy
	 * 
	 * @param filepath
	 */
	protected static void copyDirToDir(String orgPath, String destPath) {
		File srcDir = new File(orgPath);
		File destDir = new File(destPath);
		if (srcDir.exists()) {
			try {
				FileUtils.copyDirectory(srcDir, destDir);
			} catch (IOException e) {
				System.out.println("复制临时目录失败");
			}
		}
	}

	/**
	 * 删除临时目录
	 * 
	 * @param filepath
	 */
	protected static void delTempFile(String filepath) {
		File tempFile = new File(filepath);
		try {
			FileUtils.deleteDirectory(tempFile);
		} catch (IOException e) {
			System.out.println("删除临时目录失败");
		}
	}

	/**
	 * 生成本地HTML
	 * 
	 * @param url
	 *            链接地址
	 * @param filepath
	 *            生成文件路径
	 * @param filename
	 *            生成文件名称
	 * @param ecoding
	 *            编码方式
	 * @return
	 */
	private static boolean copyURLToHtml(String url, String filepath, String filename,
			String ecoding) {
		boolean flag = false;
		Document doc;
		doc = copyURLToHtmlDoc(url, filename, filepath, ecoding, 5 * 60000);// 根据Url抓取页面放入document对象
		if (null == doc) {
			System.out.println("下载页面" + url + "失败......");
			return flag;
		}
		String content = "<script type='text/javascript' src='../../render.js' charset='utf-8' ></script>"
			+ "<script type='text/javascript'>"
			+ "_RunTerrenStyle();"
			+ "</script>";
		doc = appendLink(doc, "body", content);// 在head标签中加载js
		flag = makeHtmlByDoc(filepath, filename, doc, ecoding);// 生成html文件
		System.out.println("下载页面" + url + "成功......编码: " + ecoding);
		return flag;
	}

	/**
	 * 根据url抓取网页中找到css样式,js,img链接地址及文件名
	 * 
	 * @param doc
	 *            jsoup.nodes.Document
	 * @return List<StyleSheet>
	 */
	public static List<HtmlFileLink> getHtmlFileLink(Document doc) {
		List<HtmlFileLink> HtmlFileLinks = new ArrayList<HtmlFileLink>();
		String postfix = "";// 文件后缀名
		int index = 0; // 用于文件名
		Elements importcss = doc.select("link[href]");// 找到document中带有link标签的元素
		for (Element link : importcss) {
			postfix = "css";
			if (link.attr("rel").equals("stylesheet")) {// 如果rel属性为HtmlFileLink
				String href = link.attr("abs:href");// 得到css样式的href的绝对路径
				// 如http://news.baidu.com/resource/css/search.css
				String filename = postfix + index + "." + postfix;//
				HtmlFileLinks.add(new HtmlFileLink(href, filename, postfix));
				index++;
			}
		}
		Elements media = doc.select("[src]");
		for (Element link : media) {
			if (link.ta