2008-04-12

得到特定URL网页的源代码

关键字: url source code
					
public class WebSpider {
	public static void main(String[] args) throws Exception {
		String urlString = "http://lggege.javaeye.com/blog/173840";
		URL url = new URL(urlString);
		Object contentObj = url.getContent();

		if (contentObj instanceof InputStream) {
			new InputStreamReader((InputStream) contentObj);
			BufferedReader br = new BufferedReader(new InputStreamReader((InputStream) contentObj));
			StringBuffer sb = new StringBuffer();
			while (br.ready()) {
				sb.append(br.readLine());
			}
			// 这步还需要处理编码问题.    
			System.out.println(new String(sb.toString().getBytes(), "UTF-8"));
		}
	}
}

上面是代码.

在这步:
Object contentObj = url.getContent();
是真正向URL服务器请求得到数据,也就是页面源代码.
评论
发表评论

您还没有登录,请登录后发表评论

lggege
搜索本博客
我的相册
49edb3d3-94ce-3f1d-b3b2-d2dd99204513-thumb
IndexCardGenerator
共 13 张
最近加入圈子
存档
最新评论