用java編寫一個爬蟲程序

編寫一個爬蟲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





發佈了4 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章