日期:2014-05-20 浏览次数:20870 次
public class TEST {
public static void main(String[] args) {
WebClient webClient=new WebClient(BrowserVersion.FIREFOX_17);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page;
try {
// 构造一个URL
String url = new String("http://bwc.sanxiau.edu.cn");
// 通过getPage()方法,返回相应的页面
page = (HtmlPage) webClient.getPage(url);
//写入文件//
List<HtmlAnchor> urlList = new ArrayList<HtmlAnchor>();
urlList.addAll(page.getAnchors());
Iterator<HtmlAnchor> it= urlList.iterator();
//test
String fileName = "D:\\Result2.txt";
FileWriter fw = new FileWriter(new File(fileName),true);
BufferedWriter bfw = new BufferedWriter(fw);
while(it.hasNext()){
HtmlAnchor ha = it.next();
bfw.write(ha+"\n");
}
//test
bfw.flush();
fw.flush();
bfw.close();
fw.close();
webClient.closeAllWindows();
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}