zbb20180827 java获取html内容
2021-07-13 22:07
标签:cat stack null buffer jar tps zha www end package com.zbb.test; import java.io.BufferedReader; import org.jsoup.Jsoup; public class Test02 { public static void t1(String str) { try { } zbb20180827 java获取html内容 标签:cat stack null buffer jar tps zha www end 原文地址:https://www.cnblogs.com/super-admin/p/9540758.html
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 static void main(String[] a) {
String str="https://www.baidu.com";
t1(str);
}
/**
* 需要用到 jsoup-1.7.2.jar
*
*
jsoup
* @Title: t2
* @author zhaozhen
* @param str
*/
public static void t2(String str) {
String html="";
try {
html = Jsoup.connect(str).get().html();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(html);
}
URL url = new URL(str);
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();
}
}
下一篇:Java设计模式