Java编程实现批量打印"http://www.xxx.com/xxx?ID=xxx"类型网页源码

 

import java.io.*;
import java.net.URL;
import java.net.URLConnection;



public class DownloadPage {
	public static void main(String[] args) {
		String pre = "http://www.xxx.com/User.aspx?U=";
		String suf = "";
		int start = 1;//起始ID
		int len = 999;//连续的数量
		for(int i=start; i<=start+len; i++){
			try {
				download(pre,i+"",suf);
			} catch (Throwable e) {  
		            System.out.println(e.getMessage());   
			}
		}
	}
 
	public static Object download(String prefix,String special,String suffix) throws Throwable{
		
		  URL url = new URL(prefix+special+suffix);
		  URLConnection uc =url.openConnection();
		  BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
		  String tmp = null;
		  System.out.println(url.toString());
		
		  int i = 0;
		  while((tmp=br.readLine()) != null&&i<4){ 
                        //这里打印每个页面源码的前4行,我是想获取title
			  if(tmp.trim().isEmpty())continue;
			 
			   System.out.println(tmp+"\n");
			  //将上行放在while外 可以直接打印第四行 省略前3行
			   i++;
		  }
		 
		  
		  br.close();
		 
		  return null;
	 }
}

你可能感兴趣的:(Java)