首先介绍每个类的功能: Dow nloadPage.java 的功能是下载此超链接的页面源代码. FunctionUtils.java 的功能是提供不同的静态方法,包括:页面链接正则表达式匹配,获取URL 链接的元素,判断是否创建文件,获取页面的Url 并将其转换为规范的Url,截取网页网页源文件的目标内容。 HrefOfPage.java 的功能是获取页面源代码的超链接。 UrlDataHanding.java 的功能是整合各个给类,实现 url 到获取数据到数据处理类。 UrlQueue.java 的未访问 Url 队列。 VisitedUrlQueue.java 已访问过的URL 队列。 下面介绍一下每个类的源代码: Dow nloadPage.java 此类要用到 HttpClient 组件。 1. package com.sreach.spider; 2. 3. import java.io.IOException; 4. import org.apache.http.HttpEntity; 5. import org.apache.http.HttpResponse; 6. import org.apache.http.client.ClientProtocolException; 7. import org.apache.http.client.HttpClient; 8. import org.apache.http.client.methods.HttpGet; 9. import org.apache.http.impl.client.DefaultHttpClient; 10. import org.apache.http.util.EntityUtils; 11. 12. public class DownloadPage 13. { 14. 15. /** 16. * 根据 URL 抓取网页内容 17. * 18. * @param url 19. * @return 20. */ 21. public static String getContentFormUrl(String url) 22. { 23. /* 实例化一个 HttpClient 客户端 */ 24. HttpClient client = new DefaultHttpClient(); 25. HttpGet getHttp = new HttpGet(url); 26. 27. String content = null; 28. 29. HttpResponse response; 30. try 31. { 32. /*获得信息载体 */ 33. response = client.execute(getHttp); 34. HttpEntity entity = response.getEntity(); 35. 36. VisitedUrlQueue.addElem(url); 37. 38. if (entity != null) 39. { 40. /* 转化为文本信息 */ 41. content = EntityUtils.toString(entity); 42. 43. /* 判断是否符合下载网页源代码到本地的条件 */ 44. if (FunctionUtils.isCreateFile(url) 45. && FunctionUtils.isHasGoalContent(content) != -1) 46. { 47. FunctionUtils.createFile(FunctionUtils 48. .getGoalContent(content), url); 49. } 50. } 51. 52....