java獲取網頁源碼


獲取網頁的源碼:

package com.atguigu.crud.controller; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; public class ssssss { public static void main(String[] a){ try { URL url=new URL("http://www.baidu.com"); BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter writer=new BufferedWriter(new FileWriter("index.html")); String line; while((line=reader.readLine())!=null){ System.out.println(line); writer.write(line); writer.newLine(); } reader.close(); writer.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

 還有一個更簡單的: 需要用到 jsoup-1.7.2.jar

String html = Jsoup.connect("你的網址").get().html();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM