对一些访问受限的网站进行爬取(例如需要登录、切换代理),往往需要进行自定义http请求。
BreadthCrawler默认使用JDK自带的HttpUrlConnection进行http请求,下面示例通过自定义http请求,使用httpclient 4.x进行http请求。(需要导入httpclient 4.x所需jar包,或添加httpclient 4.x的maven dependency).
import cn.edu.hfut.dmic.webcollector.crawler.BreadthCrawler;
import cn.edu.hfut.dmic.webcollector.fetcher.Fetcher;
import cn.edu.hfut.dmic.webcollector.model.CrawlDatum;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.net.HttpResponse;
import cn.edu.hfut.dmic.webcollector.net.Request;
import cn.edu.hfut.dmic.webcollector.net.Response;
import cn.edu.hfut.dmic.webcollector.parser.ParseData;
import cn.edu.hfut.dmic.webcollector.parser.ParseResult;
import cn.edu.hfut.dmic.webcollector.parser.Parser;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.nodes.Document;
/**
* 利用WebCollector进行二次开发,定义自己的爬虫
*
* @author hu
*/
public class Demo {
/**
* 自定义Http请求
*
*/
public static class MyRequest implements Request {
URL _URL;
@Override
public URL getURL() {
return _URL;
}
@Override
public void setURL(URL url) {
this._URL = url;
}
/**
* 这里采用httpclient来取代原来的方法,获取http相应,需要导入httpclient4.x的相关jar包
*/
@Override
public Response getResponse(CrawlDatum datum) throws Exception {
/*HttpResponse是一个实现Response的类*/
HttpResponse response = new HttpResponse(_URL);
/*通过httpclient来获取http请求的响应信息*/
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(getURL().toString());
/*这里用的是httpclient的HttpResponse,与WebCollector中的HttpResponse无关*/
org.apache.http.HttpResponse httpClientResponse = client.execute(httpGet);
HttpEntity entity = httpClientResponse.getEntity();
/*
将httpclient获取的http响应头信息放入Response
Response接口中要求http头是Map<String,List<String>>类型,所以需要做个转换
*/
Map<String, List<String>> headers = new HashMap<String, List<String>>();
for (Header header : httpClientResponse.getAllHeaders()) {
List<String> values = new ArrayList<String>();
values.add(header.getValue());
headers.put(header.getName(), values);
}
response.setHeaders(headers);
/*设置http响应码,必须设置http响应码,否则会影响抓取器对抓取状态的判断*/
response.setCode(httpClientResponse.getStatusLine().getStatusCode());
/*设置http响应内容,为网页(文件)的byte数组*/
response.setContent(EntityUtils.toByteArray(entity));
/*
这里返回的是HttpResponse类型,它的getContentType()方法会自动从getHeader()方法中
获取网页响应的content-type,如果自定义Response,一定要实现getContentType()方法,因
为网页解析器的生成需要依赖content-type
*/
return response;
}
}
/**
* 自定义一个广度遍历器
*/
public static class MyCrawler extends BreadthCrawler {
/**
* 覆盖Fetcher类的createRequest方法,可以自定义http请求
* 一般需要自定义一个实现Request接口的类(这里是MyRequest)
*/
@Override
public Request createRequest(String url) throws Exception {
MyRequest request = new MyRequest();
request.setURL(new URL(url));
return request;
}
/**
* 这里可以根据http响应的url和contentType来生成网页解析器 contentType可以用来区分相应是网页、图片还是文件
* 这里直接用父类的方法,可以参照父类的方法,来自己生成需要的网页解析器
*/
@Override
public Parser createParser(String url, String contentType) throws Exception {
return super.createParser(url, contentType);
}
/**
* 定义爬取成功时对页面的操作
*
* @param page
*/
@Override
public void visit(Page page) {
System.out.println("---------------------------");
/*Document是Jsoup的DOM树对象,做网页信息抽取需要依赖Document对象*/
Document doc = page.getDoc();
String title = doc.title();
System.out.println("网页URL:" + page.getUrl());
System.out.println("网页标题:" + title);
/*parseResult是在爬取过程中解析的一些简单网页信息*/
ParseResult parseResult = page.getParseResult();
/*parseData包括网页的标题、链接以及一些其他信息*/
ParseData parseData = parseResult.getParsedata();
if (parseData.getLinks() != null) {
System.out.println("网页链接数:" + parseData.getLinks().size());
}
}
}
public static void main(String[] args) throws Exception {
/*crawlPath是爬取信息存储的文件夹*/
String crawlPath = "/home/hu/data/crawl_hfut1";
MyCrawler crawler = new MyCrawler();
crawler.setCrawlPath(crawlPath);
crawler.addSeed("http://news.hfut.edu.cn/");
crawler.addRegex("http://news.hfut.edu.cn/.*");
/*禁止爬取带井号的url*/
crawler.addRegex("-.*#.*");
/*禁止爬取图片*/
crawler.addRegex("-.*png.*");
crawler.addRegex("-.*jpg.*");
crawler.addRegex("-.*gif.*");
crawler.addRegex("-.*js.*");
crawler.addRegex("-.*css.*");
/*设置线程数*/
crawler.setThreads(30);
/*设置为可断点爬取模式*/
crawler.setResumable(true);
/*进行深度为3的广度遍历*/
crawler.start(3);
}
}
分享到:
相关推荐
WebCollector爬虫、网页正文提取
爬虫webcollector 框架,所有jar包。
使用 WebCollector 的一个微博爬虫 WebCollector中提供模拟登陆的weibo.cn的功能,然后根据连接进行抓取一个用户的所有微博
Java爬虫+自定义任务可解析cron
爬虫(Web Crawler)是一种自动...爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
webcollector 开源爬虫源码包,这是2.x版本的,
试题爬取通过WebCollector爬虫框架网上爬取试题,通过Lucene全文搜索试题,通过遗传算法生成试卷 ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合...
webcollector 开源爬虫 网络搜索 实例 源码包 含可运行DEMO
考试类精品--基于SSM实现的招聘考试系统。试题爬取通过WebCollector爬虫框架网上爬取试题,通过Lucen
爬虫-Spiderman+WebCollector Spiderman2 Web Collector Spiderman2 WebCollector 爬虫-Spi derman+WebCollector 爬虫-Spiderman+WebColl ector 爬虫-Spiderman+WebCollector 爬虫-Spide rman+WebCollector
#WebCollector WebCollector爬虫框架原始码WebCollector是基于Java的开源Web爬网程序框架。它提供了一些用于爬网的简单界面,您可以在不到5分钟的时间内设置多线程Web爬网程序。 ## Homehttps ## Document ##安装...
python 反爬虫
用Scrapy爬了某美剧网站,本来不想爬的。但是这个网站广告太多了,而且最近还把一个页面分成了六个。我每次访问都要打开六个...于是,我自己做了爬虫去爬,爬完了以后,生成一个个没有广告的页面,顿时心情好了 ^_^。
WebCollector网络爬虫框架 v2.x.zip
在 Python 爬虫中,请求发送是指爬虫程序向服务器发送请求的过程。这个过程可以使用 Python 的 requests 库来实现。requests 库提供了一个简单的方式来发送 HTTP 请求,包括 GET、POST、PUT、DELETE 等方法。 3. ...
WebCollector是一个无须配置、便于二次开发的Java爬虫框架(内核),它提供精简的的API,只需少量代码即可实现一个功能强大的爬虫。WebCollector-Hadoop是WebCollector的Hadoop版本,支持分布式爬取。WebCollector...
爬虫(Web Crawler)是一种自动...爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同...
爬虫简介: WebCollector是一个无须配置、...内核具有很强的扩展性,用户可以在内核基础上开发自己想要的爬虫。源码中集成了Jsoup,可进行精准的网页解析。2.x版本中集成了selenium,可以处理javascript生成的数据。
是我第一个版本的改进,安居客租房数据的爬虫版本2.0,可以自定义爬取的页数,小白入门,记录一下自己的学习成果~