JAVA-提取页面源码Demo

原创
2016/07/26 15:01
阅读数 80
package Main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.net.URL;
public class Test {
	public static void main (String[] args) {
		getHtml("http://www.XXXX.com/");
	}
	public static void save(String name,String Lname,String data) throws IOException {
		File html=new File("./"+name+"."+Lname);
		if (!html.exists()) {
			html.createNewFile();
		}
		FileWriter fw=new FileWriter(html.getAbsoluteFile());
		BufferedWriter bw=new BufferedWriter(fw);
		bw.write(data);
		bw.close();
	}
	public static String getHtml(String target) {
		String result="";
		BufferedReader in=null;
		try{
			URL url_address=new URL(target);
			URLConnection get_url=url_address.openConnection();
                        //构造请求头
			get_url.setRequestProperty("accept", "*/*");
			get_url.setRequestProperty("connection", "Keep-Alive");
			get_url.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                        //打开请求
			get_url.connect();
			in =new BufferedReader(new InputStreamReader(get_url.getInputStream()));
			String out;
			while ((out = in.readLine()) != null) {
				result += "\n"+line;
			}
			System.out.println(result);
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(in!=null){
				try{
					in.close();
				}catch(Exception e2){
					e2.printStackTrace();
				}
			}
		}
		return result;
	}
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部