码迷,mamicode.com
首页 > 编程语言 > 详细

zbb20180827 java获取html内容

时间:2018-08-27 11:25:54      阅读:153      评论:0      收藏:0      [点我收藏+]

标签: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
*
* <dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
* @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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!