做了有关网页内容的匹配工作:
有几种方法:
1、最土的 indexof,substring,这种上学时的小打小闹明显不适合工作中使用,只解析了十几个页面就字符串下标越界了,有时只处理了3个就越界了。。所以,把整个页面传过去解析的方法明显不适合。。换一种爬取页面的方法
2、正则匹配。
String regex="<title>.*?</title>";
Pattern pat=Pattern.compile(regex);
Matcher match=pat.matcher(str);
if(match.find()){
System.out.println(match.group());
}
还是操作整个页面
3、Jsoup,前辈告我这个类。可以将网页内容分类操作,但一定要把它的jar包传到虚拟机里面,并添加路径:
export HADOOP_CLASSPATH="/usrb/hive/*:/usrb/hiveb/*:/usrb/hbase/*:/usrb/hbaseb/*:/home/clickwiseb/jsoup-1.7.3.jar"。
他还说 不要在不知道大小的情况下开辟数组空间 我有一句代码:String[] aim = new Sring[15];
http://www.jb51.net/article/43485.htm
但这个类可能会超时,解决方法是设置时长
Document doc = Jsoup.connect("http://item.yhd.com/item/33957949").timeout(5000).get (); //防止有些页面响应慢设置了延时 String title = doc.title(); System.out.println(title); Elements div=doc.select("div.crumb").select(".clearfix"); //根据div找 div class="crumb clearfix" for(Element el:div){ for(Element el1:el.select("a")){ //在找出里面的超链接a System.out.println(el1.text()); } for(Element el1:el.select("span")){ //最后一个span System.out.println(el1.text()); } }