zbb20180827 java获取html内容

2021-07-13 22:07

阅读:777

标签:cat   stack   null   buffer   jar   tps   zha   www   end   

package com.zbb.test;

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;

import org.jsoup.Jsoup;

public class Test02 {
public static void main(String[] a) {
String str="https://www.baidu.com";
t1(str);
}
/**
* 需要用到 jsoup-1.7.2.jar
*
*
org.jsoup
jsoup
1.11.3

* @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);
}

public static void t1(String str) {

try {
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();
}

}
}

zbb20180827 java获取html内容

标签:cat   stack   null   buffer   jar   tps   zha   www   end   

原文地址:https://www.cnblogs.com/super-admin/p/9540758.html


评论


亲,登录后才可以留言!