?? htmlparse.java
字號:
package com.ou;
/**
* <p>Title: 搜索引擎</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author 歐陽凱
* @version 1.0
*/
import java.util.Iterator;
import java.util.Vector;
import com.heaton.bot.HTMLPage;
import com.heaton.bot.HTTP;
import com.heaton.bot.Link;
public class HTMLParse {
HTTP _http = null;
public HTMLParse(HTTP http) {
_http = http;
}
/**
* 對Web頁面進行解析后建立索引
*/
public void start() {
try {
HTMLPage _page = new HTMLPage(_http);
_page.open(_http.getURL(), null);
Vector _links = _page.getLinks();
Index _index = new Index();
Iterator _it = _links.iterator();
int n = 0;
while (_it.hasNext()) {
Link _link = (Link) _it.next();
System.out.println(_link);
//有可能會找到xxx.cxx樣式表文件和 xxx.ico圖片文件,就會報NullPointException
//如果你運行的時候還出現了其它的不能讀取的文件在這里加進來
String link = _link.toString();
//獲得文件的后墜名
String name = link.substring(link.lastIndexOf(".")+1);
if("css".equals(name) || "ico".equals(name)){
System.out.println("是樣式表文件");
continue;
}
String _herf = input(_link.getHREF().trim());
System.out.println(_link.getPrompt().trim());
String _title = input(_link.getPrompt().trim());
System.out.println(_title);
_index.AddNews(_herf, _title);
n++;
}
System.out.println("共掃描到" + n + "條新聞");
_index.close();
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
}
/**
* 解決java中的中文問題
* @param str 輸入的中文
* @return 經過解碼的中文
*/
public static String input(String str) {
String temp = null;
if (str != null) {
try {
temp = new String(str.getBytes("ISO-8859-1"));
}
catch (Exception e) {
}
}
return temp;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -