现在的位置: 首页 > 综合 > 正文

教你如何得到特定URL网页的源代码

2018年05月19日 ⁄ 综合 ⁄ 共 576字 ⁄ 字号 评论关闭
					
public class WebSpider {
	public static void main(String[] args) throws Exception {
		String urlString = "http://lggege.iteye.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服务器请求得到数据,也就是页面源代码.

抱歉!评论已关闭.