作業部屋の使い方を試しています。
結果返却方法改良
| @@ -21,13 +21,16 @@ | ||
| 21 | 21 | */ |
| 22 | 22 | package webScraping.utility; |
| 23 | 23 | |
| 24 | +import WebScraping.core.Scraping; | |
| 24 | 25 | import webScraping.core.HtmlParser; |
| 25 | 26 | import webScraping.core.SearchData; |
| 26 | 27 | import java.awt.Desktop; |
| 27 | 28 | import java.io.File; |
| 28 | 29 | import java.io.IOException; |
| 30 | +import java.net.MalformedURLException; | |
| 29 | 31 | import java.net.URI; |
| 30 | 32 | import java.net.URISyntaxException; |
| 33 | +import java.net.URL; | |
| 31 | 34 | import java.util.logging.Level; |
| 32 | 35 | import java.util.logging.Logger; |
| 33 | 36 | import javax.swing.JFileChooser; |
| @@ -391,7 +394,7 @@ | ||
| 391 | 394 | /** |
| 392 | 395 | * 検索実行. |
| 393 | 396 | */ |
| 394 | - void Search_execution() { | |
| 397 | + void Search_execution_old() { | |
| 395 | 398 | jTxtRtn.setText(null); |
| 396 | 399 | HtmlParser par = new HtmlParser(jTxtUrl.getText()); |
| 397 | 400 |
| @@ -422,6 +425,43 @@ | ||
| 422 | 425 | } |
| 423 | 426 | |
| 424 | 427 | /** |
| 428 | + * 検索実行. | |
| 429 | + */ | |
| 430 | + void Search_execution() { | |
| 431 | + jTxtRtn.setText(null); | |
| 432 | + Scraping scrap = new Scraping(); | |
| 433 | + | |
| 434 | + URL url = null; | |
| 435 | + try { | |
| 436 | + url = new URL(jTxtUrl.getText()); | |
| 437 | + } catch (MalformedURLException ex) { | |
| 438 | + Logger.getLogger(HtmlSearch.class.getName()).log(Level.SEVERE, null, ex); | |
| 439 | + } | |
| 440 | + | |
| 441 | + SearchData[] skey = new SearchData[sdatatblmodel.getRowCount()]; | |
| 442 | + for(int row = 0; row < sdatatblmodel.getRowCount(); row++) { | |
| 443 | + skey[row] = sdatatblmodel.getSearchData(row); | |
| 444 | + } | |
| 445 | + | |
| 446 | + // HTML検索 | |
| 447 | + String[] result = scrap.getResult(url, skey); | |
| 448 | + | |
| 449 | + // 検索結果 | |
| 450 | + if(result == null) { | |
| 451 | + jTxtRtn.append("Data not find"); | |
| 452 | + return; | |
| 453 | + } | |
| 454 | + | |
| 455 | + for(int i = 0; i < skey.length; i++) { | |
| 456 | + String ans = skey[i].getitem(); | |
| 457 | + String rtn = result[i]; | |
| 458 | + jTxtRtn.append(ans + "\t" + rtn + "\n"); | |
| 459 | + } | |
| 460 | + | |
| 461 | + jTxtRtn.setCaretPosition(0); | |
| 462 | + } | |
| 463 | + | |
| 464 | + /** | |
| 425 | 465 | * @param args the command line arguments |
| 426 | 466 | */ |
| 427 | 467 | public static void main(String args[]) { |
| @@ -0,0 +1,74 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2016 kgto. | |
| 3 | + * | |
| 4 | + * This library is free software; you can redistribute it and/or | |
| 5 | + * modify it under the terms of the GNU Lesser General Public | |
| 6 | + * License as published by the Free Software Foundation; either | |
| 7 | + * version 2.1 of the License, or (at your option) any later version. | |
| 8 | + * | |
| 9 | + * This library is distributed in the hope that it will be useful, | |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 12 | + * Lesser General Public License for more details. | |
| 13 | + * | |
| 14 | + * You should have received a copy of the GNU Lesser General Public | |
| 15 | + * License along with this library; if not, write to the Free Software | |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
| 17 | + * MA 02110-1301 USA | |
| 18 | + */ | |
| 19 | +/* | |
| 20 | + * $Id: $ | |
| 21 | + */ | |
| 22 | +package WebScraping.core; | |
| 23 | + | |
| 24 | +import java.net.URL; | |
| 25 | +import webScraping.core.HtmlParser; | |
| 26 | +import webScraping.core.SearchData; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * | |
| 30 | + * @author kgto | |
| 31 | + */ | |
| 32 | +public class Scraping { | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * HTML解析. | |
| 36 | + * @param url | |
| 37 | + * @param skey | |
| 38 | + * @return | |
| 39 | + */ | |
| 40 | + public String[] getResult(URL url, SearchData[] skey) { | |
| 41 | + | |
| 42 | + HtmlParser par = new HtmlParser(url); | |
| 43 | + | |
| 44 | + String[] result = new String[skey.length]; | |
| 45 | + for(int i = 0; i < skey.length; i++) { | |
| 46 | + result[i] = par.search(skey[i]); | |
| 47 | + } | |
| 48 | + | |
| 49 | + if(!resultCheck(result)) { | |
| 50 | + return null; | |
| 51 | + } | |
| 52 | + return result; | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * 結果文字列チェック. | |
| 57 | + * @param result | |
| 58 | + * @return 文字列配列に1文字でも入力有り(null/SPACE以外)の時、true | |
| 59 | + */ | |
| 60 | + boolean resultCheck(String[] result) { | |
| 61 | + for(int i = 0; i < result.length; i++) { | |
| 62 | + String str = ""; | |
| 63 | + if(result[i] != null) { | |
| 64 | + str = result[i].trim(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + if(str.length() > 0) { | |
| 68 | + return true; | |
| 69 | + } | |
| 70 | + } | |
| 71 | + return false; | |
| 72 | + } | |
| 73 | + | |
| 74 | +} |