package test;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 下载新浪书籍
*
* @author kinkding
* @history 2009-7-14
*/
public class DownEBook {
// 新浪
// private String hrefPattern = "<a href=\"(chapter_${bookId}_\\d+\\.html)\"[^>]*title=\"(.*)\">";
// private String ctntPattern = "<div id=\"contTxt\".*?>(.*?)</div>";
// private String bookUrl = "http://vip.book.sina.com.cn/book/catalog.php?book=37495";
// QQ
private String hrefPattern = "<a href=\"javascript:opennew\\('(${rootUrl}\\d{1,2}\\.shtml)'\\)\"[\\s]?>([^<]*)</a>";
private String ctntPattern = "<div id=\"content\".*?>(.*?)</div>";
private String bookUrl = "http://book.qq.com/s/book/0/17/17572/index.shtml";
public static void main(String[] args) {
String filePath = "./tmpdown/";
DownEBook downBook = new DownEBook();
try {
downBook.startDownload(downBook.bookUrl, filePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 开始下载
*
* @param filePath
* @param bookid
* @return
* @throws IOException
*/
protected int startDownload(String strUrl, String filePath) throws IOException {
// 创建临时文件夹
File tempFile = new File(filePath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
// 下载目录文件
SimpleDateFormat sdf = new SimpleDateFormat("MMdd-HH-mm-ss");
String fileName = filePath + sdf.format(new Date()) + ".html";
downloadFile(strUrl, fileName);
String rootUrl = strUrl.substring(0, strUrl.lastIndexOf('/') + 1);
String bookId = strUrl.substring(strUrl.indexOf('=') + 1);
File f = new File(fileName);
BufferedReader reader = new BufferedReader(new FileReader(f));
String strpattern = hrefPattern.replaceAll("\\$\\{rootUrl\\}", rootUrl);
strpattern = strpattern.replaceAll("\\$\\{bookId\\}", bookId);
Pattern pattern = Pattern.compile(strpattern);
Matcher matcher = pattern.matcher("");
String line = null;
Map<String, String> fileMap = new HashMap<String, String>();
while ((line = reader.readLine()) != null) {
matcher.reset(line);
if (matcher.find()) {
System.out.println(matcher.group(1) + ", " + matcher.group(2));
fileMap.put(matcher.group(1), matcher.group(2));
}
}
reader.close();
int total = fileMap.size();
int i = 0;
for (String urlName : fileMap.keySet()) {
String name = fileMap.get(urlName);
System.out.println(++i + "/" + total + ":" + name);
urlName = urlName.startsWith("http") ? urlName : (rootUrl + urlName);
this.downloadFile(urlName, filePath + "/" + name);
this.geneTxt(filePath + "/" + name);
}
if (total > 0) {
f.delete();
}
return total;
}
/**
* 下载文件(返回文件名)
*
* @param strUrl
* @param filePath
* @return
* @throws IOException
*/
private String downloadFile(String strUrl, String fileName) throws IOException {
URL url = new URL(strUrl);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 模拟浏览器的访问
urlConn
.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1");
urlConn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
// urlConn.setRequestProperty("Accept-Encoding", "gzip,deflate");
urlConn.setRequestProperty("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
urlConn.setRequestProperty("Keep-Alive", "300");
urlConn.setRequestProperty("Connection", "keep-alive");
urlConn.setRequestProperty("Cache-Control", "max-age=0");
urlConn.setRequestProperty("Referer", strUrl);
urlConn.connect();
BufferedInputStream bis = new BufferedInputStream(urlConn.getInputStream());
FileOutputStream fos = new FileOutputStream(fileName);
byte[] buf = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
bis.close();
urlConn.disconnect();
return fileName;
}
/**
* 抽取文本
*
* @param fileName
* @throws IOException
*/
private void geneTxt(String fileName) throws IOException {
Pattern pattern = Pattern.compile(ctntPattern, Pattern.DOTALL);
File f = new File(fileName);
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = null;
StringBuffer content = new StringBuffer(512);
while ((line = reader.readLine()) != null) {
content.append(line.trim());
}
reader.close();
Matcher matcher = pattern.matcher(content);
String words = "";
if (matcher.find()) {
words = matcher.group(1);
}
if (words != null && words.length() > 0) {
f.delete();
FileWriter fw = new FileWriter(new File(fileName + ".txt"));
fw.write(words);
fw.flush();
fw.close();
}
}
}