java爬虫毕业设计难不难_爬虫入门 手写一个Java爬虫

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

page 类: 主要作用: 保存响应的相关内容 对外提供访问方法;

ppackage com.etoak.crawl.page;

import com.etoak.crawl.util.CharsetDetector;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import java.io.UnsupportedEncodingException;

/*

* page

* 1: 保存获取到的响应的相关内容;

* */

public class Page {

private byte[] content ;

private String html ; //网页源码字符串

private Document doc ;//网页Dom文档

private String charset ;//字符编码

private String url ;//url路径

private String contentType ;// 内容类型

public Page(byte[] content , String url , String contentType){

this.content = content ;

this.url = url ;

this.contentType = contentType ;

}

public String getCharset() {

return charset;

}

public String getUrl(){return url ;}

public String getContentType(){ return contentType ;}

public byte[] getContent(){ return content ;}

/**

* 返回网页的源码字符串

*

* @return 网页的源码字符串

*/

public String getHtml() {

if (html != null) {

return html;

}

if (content == null) {

return null;

}

if(charset==null){

charset = CharsetDetector.guessEncoding(content); // 根据内容来猜测 字符编码

}

try {

this.html = new String(content, charset);

return html;

} catch (UnsupportedEncodingException ex) {

ex.printStackTrace();

return null;

}

}

/*

* 得到文档

* */

public Document getDoc(){

if (doc != null) {

return doc;

}

try {

this.doc = Jsoup.parse(getHtml(), url);

return doc;

} catch (Exception ex) {

ex.printStackTrace();

return null;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值