用正则表达式抓取制定网页的特定内容(本文以抓取a标签为例)

来源:互联网 发布:linux复制文件内容 编辑:程序博客网 时间:2024/05/16 12:14
/** * 20142014年8月11日下午8:30:32 *Urls.java *home */package net.chnbs.portal.common;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;/** *  * 项目名称:TaxPortal 类名称:Urls 类描述: 创建人:wangf 创建时间:2014年8月11日 下午8:30:32 * 修改时间:2014年8月11日 下午8:30:32 修改备注: *  * @version *  */public class Urls {private String startUrl; // 开始采集网址String urlContent;String ContentArea;private String strAreaBegin, strAreaEnd; // 采集区域开始采集字符串和结束采集字符串@SuppressWarnings("unused")private String stringInUrl, stringNotInUrl;String strContent;// 获得的采集内容String[] allUrls; // 采集到的所有网址private String regex; // 采集规则UrlAndTitle urlAndTitle = new UrlAndTitle(); // 存储网址和标题public static void main(String[] args) throws IOException {Urls myurl = new Urls("<body", "/body>");myurl.getStartUrl("http://nj.jsgs.gov.cn/col/col350/index.html");myurl.getUrlContent();myurl.getContentArea();myurl.getStringInUrl("http://nj.jsgs.gov.cn/col/col350/index.html");myurl.getStringNotInUrl("google");myurl.Urls();}/* * public static void main(String[] args) { try { Document doc = * Jsoup.connect("http://nj.jsgs.gov.cn/col/col350/index.html").post(); * Elements e = doc.getElementsByClass("bt_link"); * System.out.println(e.text()); } catch (IOException e) { * e.printStackTrace(); } } */// 初始化构造函数 strAreaBegin 和strAreaEndpublic Urls(String strAreaBegin, String strAreaEnd) {this.strAreaBegin = strAreaBegin;this.strAreaEnd = strAreaEnd;}//@SuppressWarnings({ "rawtypes", "unchecked" })public List Urls() {int i = 0;final String regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*" + "class=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*target=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*"+ "title=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";final Pattern pt = Pattern.compile(regex);final Matcher mt = pt.matcher(ContentArea);Map map=null;List list=new ArrayList();while (mt.find()) {if(mt.group(3)==null||mt.group(3)==""){break;}i++;// 获取标题final Matcher title = Pattern.compile("title='([\\s\\S]*?)'").matcher(mt.group());while (title.find()) {map=new HashMap();map.put("title",title.group().replaceAll("title='|'", ""));map.put("url", "http://nj.jsgs.gov.cn"+mt.group(3));list.add(map);}}/*System.out.println("-----------------"+i+"=============="+list.toString());*/return list;}// 获得开始采集网址public void getStartUrl(String startUrl) {this.startUrl = startUrl;}// 获得网址所在内容;public void getUrlContent() {StringBuffer is = new StringBuffer();try {URL myUrl = new URL(startUrl);BufferedReader br = new BufferedReader(new InputStreamReader(myUrl.openStream(),"UTF-8"));String s;while ((s = br.readLine()) != null) {is.append(s);}urlContent = is.toString();} catch (Exception e){System.out.println("网址文件未能输出");e.printStackTrace();}}// 获得网址所在的匹配区域部分public void getContentArea() {int pos1 = 0, pos2 = 0;pos1 = urlContent.indexOf(strAreaBegin) + strAreaBegin.length();pos2 = urlContent.indexOf(strAreaEnd, pos1);ContentArea = urlContent.substring(pos1, pos2);}// 以下两个函数获得网址应该要包含的关键字及不能包含的关键字// 这里只做初步的实验。后期,保护的关键字及不能包含的关键字应该是不只一个的。public void getStringInUrl(String stringInUrl) {this.stringInUrl = stringInUrl;}public void getStringNotInUrl(String stringNotInUrl) {this.stringNotInUrl = stringNotInUrl;}// 获取采集规则// 获取url网址public void getUrl() {}public String getRegex() {return regex;}class UrlAndTitle {String myURL;String title;}}
这段代码可以得到我想要的 title和href这两个属性 ,要想获取其他属性可改变正则表达式来实现

0 0
原创粉丝点击