編寫一個爬蟲demo ,爬取XXX大學的所有新聞的標題。
<font color=''>這裏是新聞標題所在位置啦</font>
main代碼:
public static void main(String[] args) throws IOException {
URL url=new URL("你要爬的網址可貼在這兒");
BufferedReader in=new BufferedReader(
new InputStreamReader(url.openStream(),"UTF-8"));//UTF-8處理亂碼
String str;
while((str=in.readLine())!=null){
Pattern pattern = Pattern.compile("<font color=''>(.+?)</font>"); //正則表達式
// 定義一個matcher用來做匹配
Matcher matcher = pattern.matcher(str);
// 如果找到了
if (matcher.find()) {
// 打印出結果
System.out.println(matcher.group(1));
}
}
in.close();//關閉處理流
}
解析:
pattern 和matcher 的知識點
http://www.cnblogs.com/playing/archive/2011/03/15/1984943.html
附:
抓百度網頁的圖標代碼
http://www.jb51.net/article/57193.htm