闲来无事,刚学会把git部署到远程服务器,没事做,所以简单做了一个抓取网页信息的小工具,里面的一些数值如果设成参数的话可能扩展性能会更好!希望这是一个好的开始把,也让我对字符串的读取掌握的更加熟练了,值得注意的是JAVA1.8 里面在使用String拼接字符串的时候,会自动把你要拼接的字符串用StringBulider来处理,大大优化了String 的性能,闲话不多说,show my XXX code~
运行效果:
首先打开百度百科,搜索词条,比如“演员”,再按F12查看源码
然后抓取你想要的标签,注入LinkedHashMap里面就ok了,很简单是吧!看看代码罗
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6 import java.util.*; 7 8 /** 9 * Created by chunmiao on 17-3-10. 10 */ 11 public class ReadBaiduSearch { 12 13 //储存返回结果 14 private LinkedHashMap<String,String> mapOfBaike; 15 16 17 //获取搜索信息 18 public LinkedHashMap<String,String> getInfomationOfBaike(String infomationWords) throws IOException { 19 mapOfBaike = getResult(infomationWords); 20 return mapOfBaike; 21 } 22 23 //通过网络链接获取信息 24 private static LinkedHashMap<String, String> getResult(String keywords) throws IOException { 25 //搜索的url 26 String keyUrl = "http://baike.baidu.com/search?word=" + keywords; 27 //搜索词条的节点 28 String startNode = "<dl class=\"search-list\">"; 29 //词条的链接关键字 30 String keyOfHref = "href=\""; 31 //词条的标题关键字 32 String keyOfTitle = "target=\"_blank\">"; 33 34 String endNode = "</dl>"; 35 36 boolean isNode = false; 37 38 String title; 39 40 String href; 41 42 String rLine; 43 44 LinkedHashMap<String,String> keyMap = new LinkedHashMap<String,String>(); 45 46 //开始网络请求 47 URL url = new URL(keyUrl); 48 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 49 InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream(),"utf-8"); 50 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 51 52 //读取网页内容 53 while ((rLine = bufferedReader.readLine()) != null){ 54 //判断目标节点是否出现 55 if(rLine.contains(startNode)){ 56 isNode = true; 57 } 58 //若目标节点出现,则开始抓取数据 59 if (isNode){ 60 //若目标结束节点出现,则结束读取,节省读取时间 61 if (rLine.contains(endNode)) { 62 //关闭读取流 63 bufferedReader.close(); 64 inputStreamReader.close(); 65 break; 66 } 67 //若值为空则不读取 68 if (((title = getName(rLine,keyOfTitle)) != "") && ((href = getHref(rLine,keyOfHref)) != "")){ 69 keyMap.put(title,href); 70 } 71 } 72 } 73 return keyMap; 74 } 75 76 //获取词条对应的url 77 private static String getHref(String rLine,String keyOfHref){ 78 String baikeUrl = "http://baike.baidu.com"; 79 String result = ""; 80 if(rLine.contains(keyOfHref)){ 81 //获取url 82 for (int j = rLine.indexOf(keyOfHref) + keyOfHref.length();j < rLine.length()&&(rLine.charAt(j) != '\"');j ++){ 83 result += rLine.charAt(j); 84 } 85 //获取的url中可能不含baikeUrl,如果没有则在头部添加一个 86 if(!result.contains(baikeUrl)){ 87 result = baikeUrl + result; 88 } 89 } 90 return result; 91 } 92 93 //获取词条对应的名称 94 private static String getName(String rLine,String keyOfTitle){ 95 String result = ""; 96 //获取标题内容 97 if(rLine.contains(keyOfTitle)){ 98 result = rLine.substring(rLine.indexOf(keyOfTitle) + keyOfTitle.length(),rLine.length()); 99 //将标题中的内容含有的标签去掉 100 result = result.replaceAll("<em>|</em>|</a>|<a>",""); 101 } 102 return result; 103 } 104 105 }
现在都好晚了,去睡觉了...