
然后“查看元素”,整個列表頁面是通過div支撐起來的,每一條也是一個div;
其中標題和鏈接是在<div class="post_item">下的<a class="titlelnk"中;
發表時間,是在<a href=和<span class="article_comment">之間。

二、內容提取
知道了結構,下一步就是如何提取內容了。
1、提取整個列表部分
該部分相對比較簡單,找到列表部分的起始、結束部分就可以了。
如博客園精華部分的起始是:<div id="post_list">,結束是:</div>。
一般情況下,開始位置比較容易確定,但結束位置這么些用正則表達式在結束標簽是div的時候是無法獲取內容的。這種情況下要找到結束標簽的下一個標簽, 比如:
<script>editorPickStat(); aggSite.user.getUserInfo();</script>
1 // 獲得網址所在的匹配區域部分 2 3 private static String getContentArea(String urlContent, String strAreaBegin,String strAreaEnd) { 4 5 int pos1 = 0, pos2 = 0; 6 pos1 = urlContent.indexOf(strAreaBegin) + strAreaBegin.length(); 7 pos2 = urlContent.indexOf(strAreaEnd, pos1); 8 return urlContent.substring(pos1, pos2); 9 }
然后通過正則表達式獲取一個列表:
1 Pattern pt = Pattern.compile(rb.getRegex()); 2 Matcher mt = pt.matcher(contentArea); 3 4 while (mt.find()) {
2、提取單條記錄
方法同提取內容部分差不多,如單條記錄的起始標簽為<div class="post_item">,結束標簽相對來說也比較難確定。
獲取單條記錄,並去除空格之類的無用字符:
1 String rowContent = mt.group(); 2 3 rowContent = rowContent.replaceAll(rb.getRemoveRegex(), "");
3、提取出標題
取出標題用的是 >.?</a>或者title=.?>,並且要去除空格和多余的字符,需要用到類似:<li>|</li>|(<img)([\s\S]?)(>)|(<div)([\s\S]?)(>)|</div>或者(<table)([\s\S]?)(>)|<tr>|(<td)([\s\S]?)(>)|</td>|</tr>|(<table)([\s\S]*?)(>)之類的。
1 // 獲取標題 2 3 Matcher title = Pattern.compile(rb.getTitleRegex()).matcher(rowContent); 6 7 while (title.find()) { 8 9 String s = title.group().replaceAll("||>|</a>|\[.*?\]|</l>",""); 10 11 if(s ==null || s.trim().length()<=0){ 13 s = "error"; 15 } 17 tuBean.setTitle(s); 18 19 }
4、提取出超鏈接
提取出超鏈接,需要用到 href=.?target=或者href=.?.> 或者 href=.*?title。要根據實際情況配置。
1 // 獲取網址 2 3 Matcher myurl = Pattern.compile(rb.getUrlRegex()).matcher( 4 5 rowContent); 6 7 while (myurl.find()) { 8 9 String u = myurl.group().replaceAll( 10 11 "href=|"|>|target=|_blank|title", ""); 12 13 u = u.replaceAll("'|\\", ""); 14 15 if(u!=null && (u.indexOf("http://")==-1)){ 16 17 tuBean.setUrl(rb.getPrefix() + u); 18 19 }else{ 20 21 tuBean.setUrl(u); 22 23 } 24 25 } 26 27 if(tuBean.getUrl() ==null){ 28 29 tuBean.setUrl("error"); 30 31 }
5、提取出發表時間
不同的時間格式,獲取方法是不一樣的。這個也是用正則表達式獲取:比較常規的
[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}
或者:
[0-9]{4}-[0-9]{2}-[0-9]{1,2}
1 // 獲取時間 3 Matcher d = Pattern.compile(rb.getDateRegex()).matcher(rowContent); 5 while (d.find()) { 6 7 tuBean.setDeliveryDate(d.group()); 8 9 }
