欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java web页面数据抓取 博客分类: java Web数据抓取采集 

程序员文章站 2024-02-22 08:54:34
...

java抓取数据后,写入本地文件

	public static void main(String[] args){
		 URL url = null;
		 String path = null;
		 String filePath = null;
		try {
			 url =  new URL("http://publish.it168.com/2005/0915/20050915022401.shtml");
			 URLConnection urlCon = url.openConnection();
			 BufferedReader bufReader = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"gbk"));
			 
			 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
			 String dateString = formatter.format(new Date());
			 
			 SimpleDateFormat formatter1 = new SimpleDateFormat("HHmmss");
			 String dateString1 = formatter1.format(new Date());
			 
			 path = "e:/webDataGather/"+dateString;
			 File dirFile =new File(path);
			 if(!(dirFile.exists()))
				 dirFile.mkdirs();
		     
			 filePath = path+"/"+dateString1+".html";
			 BufferedWriter bufWriter = new BufferedWriter(new  FileWriter(filePath));
			 
			 copy(bufReader, bufWriter);
			 
			 bufReader.close();
			 bufWriter.close();
			 Show(filePath,"C:/Program Files/360/360se3/360SE.exe");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	public static void copy(BufferedReader read,BufferedWriter write) throws IOException{
		String str;
		StringBuilder dataStr = new StringBuilder();
		while((str=read.readLine())!=null){
			dataStr.append(str);
		}
		write.write(dataStr.toString());
		
		Pattern p = Pattern.compile("<table>.*</table>");
		Matcher m = p.matcher(dataStr.toString());
		while(m.find()){
			System.out.println(m.group());
			write.write(m.group());
		}
		
	}
	
	public static void Show(String url, String urliexplore) {
		try {
			Runtime rr = Runtime.getRuntime();
			rr.exec(urliexplore + " " + url);
		} catch (Exception er) {
		}
	}