String urlStr = "https://movie.douban.com/subject/3718279/"; // 网址 try { //创建一个url对象来指向要采集信息的网址 URL url = new URL(urlStr); //将读取到的字节转化为字符 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); InputStream a = url.openStream(); InputStreamReader inStrRead = new InputStreamReader(a, "utf-8"); //读取InputStreamReader转化成的字符 BufferedReader bufRead = new BufferedReader(inStrRead); //读到的内容不为空 String line; while ((line = bufRead.readLine()) != null) { System.out.println(line); } bufRead.close(); } catch (IOException e) { e.printStackTrace(); System.out.println(e.toString()); }
java获取页面源代码
最新推荐文章于 2022-10-11 14:18:42 发布