java web页面数据抓取

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) {
		}
	}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值