首页 > 代码库 > Java HTML页面抓取实例
Java HTML页面抓取实例
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class Url { public static void main(String[] args) throws Exception{ String html = getURLContent(); System.out.println(html); } /** * 获取网页内容 */ private static String getURLContent() throws MalformedURLException, IOException, UnsupportedEncodingException { URL urlmy = new URL("http://www.baidu.com"); HttpURLConnection con = (HttpURLConnection) urlmy.openConnection(); HttpURLConnection.setFollowRedirects(true); con.setInstanceFollowRedirects(false); con.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8")); String s = ""; StringBuffer sb = new StringBuffer(); while ((s = br.readLine()) != null) { sb.append(s+"\r\n"); } return sb.toString(); }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。