一个简易的网页爬虫,可用于下载在线API文档
2012-12-13 09:52
513 查看
package wkx; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; import java.util.Set; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; public class Main { private static Set<String> filenames = new HashSet<String>(); public static String getSource(String url) { String response = null; HttpClient client = new HttpClient(); HttpMethod method = null; try { method = new PostMethod(url); client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } } catch (IOException e) { System.out.println("Get Source Error!"); } finally { if (method != null) method.releaseConnection(); } return response; } public static void create(String url, String froot, String cur) { String curUrl = url + "/" + cur; if (filenames.contains(curUrl)) { return; } filenames.add(curUrl); String cont = getSource(curUrl); if (cont == null) { return; } File f = null; FileOutputStream fos = null; try { f = new File(froot); if (!f.exists()) { f.mkdirs(); } f = new File(froot + "\\" + cur); f.createNewFile(); fos = new FileOutputStream(f); fos.write(cont.getBytes()); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } String[] files = cont.split("\""); for (String file : files) { if (file.matches(".*\\.html$") && !file.startsWith("http")) { if (file.contains(" ")) continue; String turl = url; String tfroot = froot; file.replaceAll("//", "/"); int tindex = file.lastIndexOf("?"); if (tindex != -1) { file = file.substring(0, tindex); } int index = 0; while ((index = file.indexOf("/")) != -1) { if (file.charAt(index - 1 < 0 ? 0 : index - 1) == '.') { turl = turl.substring(0, turl.lastIndexOf("/")); tfroot = tfroot.substring(0, tfroot.lastIndexOf("\\")); } else { turl = turl + "/" + file.substring(0, index); tfroot = tfroot + "\\" + file.substring(0, index); } file = file.substring(index + 1); } create(turl, tfroot, file); } } } public static void main(String[] args) { String url = "http://localhost"; String froot = "C:\\Users\\Jack_Wong\\Desktop\\api"; create(url, froot, "index.html"); } }
相关文章推荐
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- python一个下载小说的简易爬虫
- jQuery 1.7 正式版已经可以下载使用。jQuery是一个JavaScript库,它简化了HTML文档遍历,事件处理,动画和为网络快速发展的Ajax交互。jQuery 1.7 版本加入了新的事件API .on() 和 .off(),提
- 动手写一个简易的文档下载Chrome插件
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- JAVA API 1.6下载和在线文档
- 爬虫系列6下载一个网页(异常处理,用户代理,重试次数)
- python 爬虫(二)一个带下载进度的网页抓取小爬虫
- 一个小爬虫 从网页获取信息(图片下载)
- 用 HTML5 和 Javascript 仿制一个水果忍者网页版! (直接浏览器在线玩+源代码下载)
- 小爬虫-一个网页上的.jpg图片下载下来
- 使用正则表达式写一个网页爬虫案例获取指定文档中的邮件地址保存到自己指定的文件夹中
- 用 HTML5 和 Javascript 仿制一个水果忍者网页版! (直接浏览器在线玩+源代码下载)
- 一个好的在线工具网站,里面有很多API文档(很好)
- 借助RESTful API,用Excel开发网站,可用于多人在线实时编辑Excel,有实例下载
- Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫
- 一个可以在线编写API文档和说明文档的php应用
- 通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界
- 一个非常适合IT团队的在线API文档、技术文档工具 (ShowDoc)