国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

一個簡易的Java多頁面隊列爬蟲程序

2019-11-26 14:00:29
字體:
供稿:網(wǎng)友

之前寫過很多單頁面python爬蟲,感覺python還是很好用的,這里用java總結一個多頁面的爬蟲,迭代爬取種子頁面的所有鏈接的頁面,全部保存在tmp路徑下。  

一、 序言

實現(xiàn)這個爬蟲需要兩個數(shù)據(jù)結構支持,unvisited隊列(priorityqueue:可以適用pagerank等算法計算出url重要度)和visited表(hashset:可以快速查找url是否存在);隊列用于實現(xiàn)寬度優(yōu)先爬取,visited表用于記錄爬取過的url,不再重復爬取,避免了環(huán)。java爬蟲需要的工具包有httpclient和htmlparser1.5,可以在maven repo中查看具體版本的下載。
1、目標網(wǎng)站:新浪  http://www.sina.com.cn/
2、結果截圖: 

下面說說爬蟲的實現(xiàn),后期源碼會上傳到github中,需要的朋友可以留言:

二、爬蟲編程 
1、創(chuàng)建種子頁面的url
 MyCrawler crawler = new MyCrawler();
crawler.crawling(new String[]{"
http://www.sina.com.cn/"});

2、初始化unvisited表為上面的種子url
LinkQueue.addUnvisitedUrl(seeds[i]);

3、最主要的邏輯實現(xiàn)部分:在隊列中取出沒有visit過的url,進行下載,然后加入visited的表,并解析改url頁面上的其它url,把未讀取的加入到unvisited隊列;迭代到隊列為空停止,所以這個url網(wǎng)絡還是很龐大的。注意,這里的頁面下載和頁面解析需要java的工具包實現(xiàn),下面具體說明下工具包的使用。 

while(!LinkQueue.unVisitedUrlsEmpty()&&LinkQueue.getVisitedUrlNum()<=1000)  {   //隊頭URL出隊列   String visitUrl=(String)LinkQueue.unVisitedUrlDeQueue();   if(visitUrl==null)    continue;   DownLoadFile downLoader=new DownLoadFile();   //下載網(wǎng)頁   downLoader.downloadFile(visitUrl);   //該 url 放入到已訪問的 URL 中   LinkQueue.addVisitedUrl(visitUrl);   //提取出下載網(wǎng)頁中的 URL      Set<String> links=HtmlParserTool.extracLinks(visitUrl,filter);   //新的未訪問的 URL 入隊   for(String link:links)   {     LinkQueue.addUnvisitedUrl(link);   }  }

4、下面html頁面的download工具包 

public String downloadFile(String url) {  String filePath = null;  /* 1.生成 HttpClinet 對象并設置參數(shù) */  HttpClient httpClient = new HttpClient();  // 設置 Http 連接超時 5s  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(    5000);  /* 2.生成 GetMethod 對象并設置參數(shù) */  GetMethod getMethod = new GetMethod(url);  // 設置 get 請求超時 5s  getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);  // 設置請求重試處理  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,    new DefaultHttpMethodRetryHandler());  /* 3.執(zhí)行 HTTP GET 請求 */  try {   int statusCode = httpClient.executeMethod(getMethod);   // 判斷訪問的狀態(tài)碼   if (statusCode != HttpStatus.SC_OK) {    System.err.println("Method failed: "      + getMethod.getStatusLine());    filePath = null;   }   /* 4.處理 HTTP 響應內(nèi)容 */   byte[] responseBody = getMethod.getResponseBody();// 讀取為字節(jié)數(shù)組   // 根據(jù)網(wǎng)頁 url 生成保存時的文件名   filePath = "temp//"     + getFileNameByUrl(url, getMethod.getResponseHeader(       "Content-Type").getValue());   saveToLocal(responseBody, filePath);  } catch (HttpException e) {   // 發(fā)生致命的異常,可能是協(xié)議不對或者返回的內(nèi)容有問題   System.out.println("Please check your provided http address!");   e.printStackTrace();  } catch (IOException e) {   // 發(fā)生網(wǎng)絡異常   e.printStackTrace();  } finally {   // 釋放連接   getMethod.releaseConnection();  }  return filePath; }

5、html頁面的解析工具包: 

public static Set<String> extracLinks(String url, LinkFilter filter) {  Set<String> links = new HashSet<String>();  try {   Parser parser = new Parser(url);   parser.setEncoding("gb2312");   // 過濾 <frame >標簽的 filter,用來提取 frame 標簽里的 src 屬性所表示的鏈接   NodeFilter frameFilter = new NodeFilter() {    public boolean accept(Node node) {     if (node.getText().startsWith("frame src=")) {      return true;     } else {      return false;     }    }   };   // OrFilter 來設置過濾 <a> 標簽,和 <frame> 標簽   OrFilter linkFilter = new OrFilter(new NodeClassFilter(     LinkTag.class), frameFilter);   // 得到所有經(jīng)過過濾的標簽   NodeList list = parser.extractAllNodesThatMatch(linkFilter);   for (int i = 0; i < list.size(); i++) {    Node tag = list.elementAt(i);    if (tag instanceof LinkTag)// <a> 標簽    {     LinkTag link = (LinkTag) tag;     String linkUrl = link.getLink();// url     if (filter.accept(linkUrl))      links.add(linkUrl);    } else// <frame> 標簽    {     // 提取 frame 里 src 屬性的鏈接如 <frame src="test.html"/>     String frame = tag.getText();     int start = frame.indexOf("src=");     frame = frame.substring(start);     int end = frame.indexOf(" ");     if (end == -1)      end = frame.indexOf(">");     String frameUrl = frame.substring(5, end - 1);     if (filter.accept(frameUrl))      links.add(frameUrl);    }   }  } catch (ParserException e) {   e.printStackTrace();  }  return links; }

6、未訪問頁面使用PriorityQueue帶偏好的隊列保存,主要是為了適用于pagerank等算法,有的url忠誠度更高一些;visited表采用hashset實現(xiàn),注意可以快速查找是否存在; 

public class LinkQueue { //已訪問的 url 集合 private static Set visitedUrl = new HashSet(); //待訪問的 url 集合 private static Queue unVisitedUrl = new PriorityQueue(); //獲得URL隊列 public static Queue getUnVisitedUrl() {  return unVisitedUrl; } //添加到訪問過的URL隊列中 public static void addVisitedUrl(String url) {  visitedUrl.add(url); } //移除訪問過的URL public static void removeVisitedUrl(String url) {  visitedUrl.remove(url); } //未訪問的URL出隊列 public static Object unVisitedUrlDeQueue() {  return unVisitedUrl.poll(); } // 保證每個 url 只被訪問一次 public static void addUnvisitedUrl(String url) {  if (url != null && !url.trim().equals("") && !visitedUrl.contains(url)    && !unVisitedUrl.contains(url))   unVisitedUrl.add(url); } //獲得已經(jīng)訪問的URL數(shù)目 public static int getVisitedUrlNum() {  return visitedUrl.size(); } //判斷未訪問的URL隊列中是否為空 public static boolean unVisitedUrlsEmpty() {  return unVisitedUrl.isEmpty(); }}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凉山| 许昌县| 清原| 金湖县| 突泉县| 巴马| 柘荣县| 临江市| 鸡东县| 四会市| 绥宁县| 泸西县| 中阳县| 恩施市| 日照市| 云安县| 舞钢市| 平山县| 类乌齐县| 南丹县| 九江县| 苏尼特右旗| 南通市| 高陵县| 青浦区| 蕉岭县| 恩施市| 新化县| 理塘县| 海林市| 冀州市| 苏尼特左旗| 察隅县| 芦山县| 弥渡县| 南乐县| 安龙县| 抚松县| 车险| 宁河县| 柞水县|