日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java实现网页保存_详解Java两种方式简单实现:爬取网页并且保存

發(fā)布時(shí)間:2023/12/4 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现网页保存_详解Java两种方式简单实现:爬取网页并且保存 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

對(duì)于網(wǎng)絡(luò),我一直處于好奇的態(tài)度。以前一直想著寫(xiě)個(gè)爬蟲(chóng),但是一拖再拖,懶得實(shí)現(xiàn),感覺(jué)這是一個(gè)很麻煩的事情,出現(xiàn)個(gè)小錯(cuò)誤,就要調(diào)試很多時(shí)間,太浪費(fèi)時(shí)間。

后來(lái)一想,既然早早給自己下了保證,就先實(shí)現(xiàn)它吧,從簡(jiǎn)單開(kāi)始,慢慢增加功能,有時(shí)間就實(shí)現(xiàn)一個(gè),并且隨時(shí)優(yōu)化代碼。

下面是我簡(jiǎn)單實(shí)現(xiàn)爬取指定網(wǎng)頁(yè),并且保存的簡(jiǎn)單實(shí)現(xiàn),其實(shí)有幾種方式可以實(shí)現(xiàn),這里慢慢添加該功能的幾種實(shí)現(xiàn)方式。

UrlConnection爬取實(shí)現(xiàn)

package html;

import java.io.BufferedReader;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

public class Spider {

public static void main(String[] args) {

String filepath = "d:/124.html";

String url_str = "http://www.hao123.com/";

URL url = null;

try {

url = new URL(url_str);

} catch (MalformedURLException e) {

e.printStackTrace();

}

String charset = "utf-8";

int sec_cont = 1000;

try {

URLConnection url_con = url.openConnection();

url_con.setDoOutput(true);

url_con.setReadTimeout(10 * sec_cont);

url_con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");

InputStream htm_in = url_con.getInputStream();

String htm_str = InputStream2String(htm_in,charset);

saveHtml(filepath,htm_str);

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* Method: saveHtml

* Description: save String to file

* @param filepath

* file path which need to be saved

* @param str

* string saved

*/

public static void saveHtml(String filepath, String str){

try {

/*@SuppressWarnings("resource")

FileWriter fw = new FileWriter(filepath);

fw.write(str);

fw.flush();*/

OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(filepath, true), "utf-8");

outs.write(str);

System.out.print(str);

outs.close();

} catch (IOException e) {

System.out.println("Error at save html...");

e.printStackTrace();

}

}

/**

* Method: InputStream2String

* Description: make InputStream to String

* @param in_st

* inputstream which need to be converted

* @param charset

* encoder of value

* @throws IOException

* if an error occurred

*/

public static String InputStream2String(InputStream in_st,String charset) throws IOException{

BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));

StringBuffer res = new StringBuffer();

String line = "";

while((line = buff.readLine()) != null){

res.append(line);

}

return res.toString();

}

}

實(shí)現(xiàn)過(guò)程中,爬取的網(wǎng)頁(yè)的中文亂碼問(wèn)題,是個(gè)比較麻煩的事情。

HttpClient爬取實(shí)現(xiàn)

HttpClient實(shí)現(xiàn)爬取網(wǎng)頁(yè)時(shí),遇到了很多問(wèn)題。其一,就是存在兩個(gè)版本的HttpClient,一個(gè)是sun內(nèi)置的,另一個(gè)是apache開(kāi)源的一個(gè)項(xiàng)目,似乎sun內(nèi)置用的不太多,我也就沒(méi)有實(shí)現(xiàn),而是采用了apache開(kāi)源項(xiàng)目(以后說(shuō)的HttpClient都是指apache的開(kāi)源版本);其二,在使用HttpClient時(shí),最新的版本已經(jīng)不同于以前的版本,從HttpClient4.x版本后,導(dǎo)入的包就已經(jīng)不一樣了,從網(wǎng)上找的很多部分都是HttpClient3.x版本的,所以如果使用最新的版本,還是看幫助文件為好。

我用的是Eclipse,需要配置環(huán)境導(dǎo)入引用包。

首先,下載HttpClient,地址是:http://hc.apache.org/downloads.cgi,我是用的事HttpClient4.2版本。

然后,解壓縮,找到了/lib文件夾下的commons-codec-1.6.jar,commons-logging-1.1.1.jar,httpclient-4.2.5.jar,httpcore-4.2.4.jar(版本號(hào)根據(jù)下載的版本有所不同,還有其他的jar文件,我這里暫時(shí)用不到,所以先導(dǎo)入必須的);

最后,將上面的jar文件,加入classpath中,即右擊工程文件 => Bulid Path => Configure Build Path => Add External Jar..,然后添加上面的包就可以了。

還用一種方法就是講上面的包,直接復(fù)制到工程文件夾下的lib文件夾中。

下面是實(shí)現(xiàn)代碼:

package html;

import java.io.BufferedReader;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.*;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

public class SpiderHttpClient {

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

String url_str = "http://www.hao123.com";

String charset = "utf-8";

String filepath = "d:/125.html";

HttpClient hc = new DefaultHttpClient();

HttpGet hg = new HttpGet(url_str);

HttpResponse response = hc.execute(hg);

HttpEntity entity = response.getEntity();

InputStream htm_in = null;

if(entity != null){

System.out.println(entity.getContentLength());

htm_in = entity.getContent();

String htm_str = InputStream2String(htm_in,charset);

saveHtml(filepath,htm_str);

}

}

/**

* Method: saveHtml

* Description: save String to file

* @param filepath

* file path which need to be saved

* @param str

* string saved

*/

public static void saveHtml(String filepath, String str){

try {

/*@SuppressWarnings("resource")

FileWriter fw = new FileWriter(filepath);

fw.write(str);

fw.flush();*/

OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(filepath, true), "utf-8");

outs.write(str);

outs.close();

} catch (IOException e) {

System.out.println("Error at save html...");

e.printStackTrace();

}

}

/**

* Method: InputStream2String

* Description: make InputStream to String

* @param in_st

* inputstream which need to be converted

* @param charset

* encoder of value

* @throws IOException

* if an error occurred

*/

public static String InputStream2String(InputStream in_st,String charset) throws IOException{

BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));

StringBuffer res = new StringBuffer();

String line = "";

while((line = buff.readLine()) != null){

res.append(line);

}

return res.toString();

}

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的java实现网页保存_详解Java两种方式简单实现:爬取网页并且保存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。