国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java應用開源框架實現簡易web搜索引擎

Java應用開源框架實現簡易web搜索引擎

2021-02-23 10:35lannooooooooooo Java教程

本篇文章主要介紹了Java應用開源框架實現簡易web搜索引擎,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

引言

應用 Java 的開源庫,編寫一個搜索引擎,這個引擎能爬取一個網站的內容。并根據網頁內容進行深度爬取,獲取所有相關的網頁地址和內容,用戶可以通過關鍵詞,搜索所有相關的網址。

具體功能

(1) 用戶可以指定爬取一個url對應的網頁的內容。
(2) 對網頁內容進行解析,并獲取其中所有的url鏈接地址。
(3) 用戶可以設定爬取深度,代表著從初始url對應的頁面開始,可以爬取其中所有的url對應的網頁內的url,以此類推。深度越大,能爬取到的網站越多。
(4) 對爬取到的url內容進行保存、建立索引。建立索引的內容是url地址本身,和url對應的網頁標題。
(5) 用戶可以通過關鍵詞對網址進行搜索,找出有該關鍵詞的url地址。
(6) 建立索引和搜索索引的過程能智能識別中文關鍵詞,能對關鍵詞進行分詞操作。
(7) 用戶可以指定保存索引的地址、初始url、爬取深度、進行搜索的關鍵詞和最大匹配項。

開源框架

  1. Lucene
  2. Jsoup

源碼

爬蟲部分:Spider.java

?
<abbr id="8mqqs"></abbr>
<dl id="8mqqs"><dd id="8mqqs"></dd></dl><abbr id="8mqqs"><menu id="8mqqs"></menu></abbr>
<abbr id="8mqqs"><menu id="8mqqs"></menu></abbr>
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    package webCrawler.Spider;
     
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Scanner;
     
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
     
    import webCrawler.Index.BuildIndex;
     
    /**
     * @author lannooo
     */
     
    public class Spider {
      ArrayList<String> URLs;
      private String startURL;
      private int digLevel;
     
      /**
       * @param startURL 爬蟲的起始URL
       * @param digLevel 爬取深度
       */
      public Spider(String startURL, int digLevel){
        this.startURL = startURL;
        this.digLevel = digLevel;
        this.URLs = new ArrayList<>();
      }
     
      /**
       * @param level 當前爬取的深度剩余
       * @param arrayList 需要進行下一輪爬去的URL集
       * @return 從一格url集爬取到的新的URL集
       * @throws IOException
       */
      public ArrayList<String> getLevelURLs(int level, ArrayList<String> arrayList)
          throws IOException{
        ArrayList<String> total = null;
        if(level>0){     
          total = new ArrayList<>();
          for(String url: arrayList){
            /*對于每個arrayList中的URL,首先解析其網頁內容,并獲得里面所有URL項*/
            for(String each: getBareLinks(url)){
              total.add(each);
            }
          }
          /*用HashSet這個容器將total里面重復項刪除*/
          HashSet<String> hashSet = new HashSet<>(total);
          total = new ArrayList<>(hashSet);
        }
        return total;
      }
     
      /**
       * 從startURL開始,爬取所有相關URLs
       * @throws IOException
       */
      public void getAll() throws IOException{
        ArrayList<String> newURLs;
        ArrayList<String> currentURLs = new ArrayList<>();
        /*把startURL加入currentURLs這個列表中,從這個url開始爬*/
        currentURLs.add(startURL);
        for(int i=digLevel; i>0; i--){
          /*
           * 對于每一層,都要獲取一次由這個url引申出去的url集
           * 然后把當前集的已經爬去過的url加入到總的URL集中
           * 最后newURLs作為新的需要進行深度爬取的集進入下一輪循環
           */
          System.out.println("Dig into level: " + (digLevel-i+1));
          newURLs = getLevelURLs(i, currentURLs);
          for(String each: currentURLs){
            URLs.add(each);
          }
          currentURLs = newURLs;
        }
        for(String each:currentURLs){
          URLs.add(each);
        }
        HashSet<String> hashSet = new HashSet<>(URLs);
        URLs = new ArrayList<>(hashSet);
      }
     
      /**
       * @param path 保存索引的路徑
       * @throws IOException
       */
      public void storeURLsAndInfo(String path) throws IOException{
        BuildIndex build = new BuildIndex(path);
        /* 把URLs中的所有url進行實際網頁標題的爬取*/
        for(String each:URLs){
          String text = getLinkText(each);
          if(text!=null){
            build.addField("url", each);
            build.addField("text", text);
            /*將這一個entry加入索引中*/
            build.pushIndex();
          }
        }
        build.close();
      }
     
      /**
       * @param url 需要獲取網頁標題的url
       * @return 標題內容
       * @throws IOException
       */
      public String getLinkText(String url) throws IOException{
        Document document = null;
        try {
          /*用Jsoup進行連接,設置超時時間為3秒*/
          document = Jsoup.connect(url).timeout(3000).get();
        } catch (Exception e) {
          System.out.println("[TIMEOUT]Get title of url:"+url);
          return null;
        }
        String id="codetool">

    建立索引:BuildIndex.java

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    package webCrawler.Index;
     
    import java.io.*;
     
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.document.TextField;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    import org.wltea.analyzer.lucene.IKAnalyzer;
     
    /**
     * @author lannooo
     *
     */
    public class BuildIndex {
      private File file;
      private Directory directory;
      private IndexWriter indexWriter;
      private IndexWriterConfig config;
      private Analyzer analyzer;
      private Document document;
     
      /**
       * @param path 建立索引的路徑
       */
      public BuildIndex(String path) {
        try {
          file = new File(path);
          directory = FSDirectory.open(file);
          document = new Document();
          analyzer = new IKAnalyzer();    /*中文分詞工具類*/
          config = new IndexWriterConfig(Version.LUCENE_4_10_0, analyzer);
          indexWriter = new IndexWriter(directory, config);     
     
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
     
      /**
       * @param fieldName 加入到document中的新的一項的名稱
       * @param fieldText 新的一項的內容
       */
      public void addField(String fieldName, String fieldText){
        try{
          Field field = new TextField(fieldName, fieldText, Field.Store.YES);
          document.add(field);
        }catch (Exception e) {
          e.printStackTrace();
        }
      }
     
      /**
       * 將document加入到索引中
       */
      public void pushIndex(){
        try {
          indexWriter.addDocument(document);
          document = new Document();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
     
      /**
       * 加入完整的一個document并保存到索引中
       * @param url 加入的url地址
       * @param text url對應的文本
       */
      public void addOneIndex(String url, String text){
        this.addField("url", url);
        this.addField("text", text);
        this.pushIndex();
      }
     
      /**
       * 關閉索引寫入
       */
      public void close(){
        try {
          indexWriter.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
     
    }

    搜索索引

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    package webCrawler.Index;
     
    import java.io.File;
    import java.util.Scanner;
     
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.index.DirectoryReader;
    import org.apache.lucene.queryparser.classic.QueryParser;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.FSDirectory;
    import org.wltea.analyzer.lucene.IKAnalyzer;
     
    /**
     * @author lannooo
     *
     */
    public class SearchIndex {
      private IndexSearcher indexSearcher;
      private Analyzer analyzer;
      private QueryParser parser;
      private Query query;
      private TopDocs hits;
      private DirectoryReader reader;
     
      /**
       * @param path 進行索引搜索的路徑
       */
      public SearchIndex(String path){
        try {
          reader = DirectoryReader.open(FSDirectory.open(new File(path)));
          indexSearcher = new IndexSearcher(reader);
          analyzer = new IKAnalyzer();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
     
      /**
       * @param fieldName 搜索的域名稱
       * @param text 搜索的內容
       * @param matchNumber 最大匹配項數
       * @return 搜索到的最大匹配數
       */
      public int search(String fieldName, String text, int matchNumber){
        try {
          parser = new QueryParser(fieldName, analyzer);
          query = parser.parse(text);
          hits = indexSearcher.search(query, matchNumber);
     
          return hits.totalHits;
        } catch (Exception e) {
          e.printStackTrace();
        }
        return -1;
      }
      /**
       * 打印所有的匹配項
       */
      public void printHits(){
        try{
          System.out.println("Total hits number:"+hits.totalHits);
          for(ScoreDoc doc: hits.scoreDocs){
            Document document = indexSearcher.doc(doc.doc);
            System.out.println(document.get("url"));
            System.out.println(document.get("text"));
          }
          reader.close();
        }catch (Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        /*輸入關鍵詞*/
        Scanner in = new Scanner(System.in);
        System.out.println("Enter path of the index:");
        String path = in.nextLine().trim();
        while(path.length()==0){
          System.out.println("Enter path of the index:");
          path = in.nextLine().trim();
        }
     
        System.out.println("Enter max hit number:");
        int max = in.nextInt();
        while(max<0){
          System.out.println("Enter max hit number:");
          max = in.nextInt();
        }
        in.nextLine();
        System.out.print("Search>>> ");
        String text = in.nextLine().trim();
        /*循環讀入用戶的關鍵詞,如果是q則退出,長度為0也退出*/
        while(!text.equals("q")){
          if(text.length()>0){
            SearchIndex search = new SearchIndex(path);
            int hits = search.search("text", text, max);
            if(hits!=-1){
              search.printHits();
            }
          }
          System.out.print("Search>>> ");
          text = in.nextLine().trim();
        }
      }
    }

    UI界面(這里為了方便只是命令行的形式,可以根據需求寫一個GUI界面)

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package webCrawler.UI;
     
    import java.util.Scanner;
     
    import webCrawler.Index.SearchIndex;
     
    /**
     * @author lannooo
     *
     */
    public class UI {
      public static void main(String[] args) {
        /*輸入關鍵詞*/
        Scanner in = new Scanner(System.in);
        System.out.print("Search>>> ");
        String text = in.nextLine().trim();
        /*對于用戶的關鍵詞,如果是q則退出,長度為0也退出*/
        while(!text.equals("q") && text.length()>0){
          SearchIndex search = new SearchIndex("d:/index-spider2");
          int hits = search.search("text", text, 20);
          if(hits!=-1){
            search.printHits();
          }
          System.out.print("Search>>> ");
          text = in.nextLine().trim();
        }
      }
    }

    以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

    原文鏈接:http://blog.csdn.net/qq_22187919/article/details/60466006

    延伸 · 閱讀

    精彩推薦
    主站蜘蛛池模板: 欧美日韩亚洲一区二区三区 | 欧美精品一区二区三区在线 | 精品国产欧美一区二区 | 久久伊人一区 | 国内自拍视频在线观看 | 亚洲2020天天堂在线观看 | 久久综合一区二区 | 成人免费xxx在线观看 | 精品无码久久久久国产 | 在线永久免费观看黄网站 | 亚洲国产精品免费 | 五月婷婷综合网 | 中文字幕 亚洲一区 | 99视频免费| 亚洲精品日韩在线 | 久草成人网 | 黄视频| 中文一区 | 午夜影院a | 日韩一区电影 | 一级黄色a毛片 | 久久av网 | 91观看| 一区在线视频 | 久在线| 久久久一区二区三区 | 91夜色 | 成人影院在线 | 91偷拍精品一区二区三区 | 免费不卡视频 | 四虎免费看黄 | 亚洲一区二区免费看 | 免费欧美 | 亚洲国产成人av好男人在线观看 | 人一级毛片 | 国产午夜精品一区二区三区嫩草 | 黄色电影在线免费观看 | 欧美狠狠操| 狠狠操狠狠操 | 免费一级毛片观看 | 午夜精品久久久久久久男人的天堂 |
      <center id="8mqqs"></center>
      <strike id="8mqqs"><dd id="8mqqs"></dd></strike>
      <bdo id="8mqqs"></bdo>
    • <pre id="8mqqs"></pre>