a code snip
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import com.creditcloud.brick.task.CrawlTask;
import com.creditcloud.brick.task.Extractor;
import com.creditcloud.brick.task.Field; @NoArgsConstructor
@AllArgsConstructor
@Data
@Slf4j
public class DataCrawler {
String url;
String space; public HashMap<String, Object> doCrawl(CrawlTask task ) {
HashMap<String, Object> result = new HashMap<String, Object>();
this.url = task.getUrl();
this.space = task.getId();
//
String content=this.doGet(url);
if( StringUtils.isNotEmpty(content)) {
Extractor actor=task.getExtractor();
if( actor != null )
this.parse(actor, content, result);
}
return result;
} public String doGet( String url ) {
String data=null;
//return Jsoup.connect(url).userAgent("Mozilla").get();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet hGet = new HttpGet(url);
log.info(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(hGet);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
log.info( response.getStatusLine().toString() );
//
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
data = EntityUtils.toString(entity);
System.out.println(data);
EntityUtils.consume(entity); }
}
catch(Exception e){
log.error(e.getMessage());
}
//response
try
{
if( response != null )
response.close();
}
catch(Exception e) {
log.error(e.getMessage());
}
//httpclient
try {
if( httpclient != null )
httpclient.close();
} catch (Exception e) {
log.error(e.getMessage());
} return data;
} public String removeHtmlLabel( String input ) {
return input.replaceAll("<[^>]+>", "").replaceAll(" "," ").trim();
} //
public ArrayList<String> match( Extractor extractor, String input ) {
ArrayList<String> result = new ArrayList<String>();
switch (extractor.getType()) {
case css: //call css
{
Document doc = Jsoup.parse(input);
Elements elems = doc.select(extractor.getPattern());
for( Element elem:elems ) {
result.add( elem.toString() );
}
}
break;
case regex: //call regex
{
Pattern p = Pattern.compile( extractor.getPattern());
Matcher m = p.matcher( input );
String matchValue = null;
while(m.find()) {
matchValue = StringEscapeUtils.unescapeHtml4( m.group());
result.add(matchValue);
}
}
break;
case empty:
result.add(input);
break;
}
return result;
} public void parse( Extractor extractor, String input, HashMap<String, Object> result ) {
//1. match by css or regex
ArrayList<String> strlist = this.match(extractor, input);
if( strlist.isEmpty() ) {
//result.put( extractor.getId(), null);
return;
}
//2. call children extractors
switch(extractor.getData()) {
case array:{
//result.setType(ResultDataType.array);
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
for( String str:strlist ) {
HashMap<String, Object> childResult = new HashMap<String, Object>();
for( Extractor one:extractor.getChildren()) {
this.parse(one, str, childResult);
}
if( childResult.isEmpty() == false )
list.add(childResult);
}
if(list.isEmpty() == false )
result.put( extractor.getId(), list );
}
break;
case field:{
for(Field fd:extractor.getFields()) {
String val=strlist.get( fd.getIndex() );
result.put( fd.getName(), this.removeHtmlLabel(val) );
}
}
break;
case none: {
for( String str:strlist ) {
for( Extractor one:extractor.getChildren()) {
this.parse(one, str, result);
}
}
}
break;
}
}
}
a code snip的更多相关文章
- CSS code snip enjoy.
<!-- information-total得是动态获取吧. --> <div class="information-mod"> <div class ...
- C# Code Snip
1.Tryf + TAB+TAB try { } finally { } 2.Prop+Tab+Tab public int MyProperty { get; set; } 3. #region + ...
- WPF整理-跨程序集访问资源
“Sometimes binary resources are defined in one assembly (typically a class library), but areneeded i ...
- WPF整理-使用用户选择主题的颜色和字体
“Sometimes it's useful to use one of the selected colors or fonts the user has chosen in theWindows ...
- WPF整理-XAML访问静态属性
"XAML provides an easy way to set values of properties—type converters and the extended propert ...
- WPF整理-XAML构建后台类对象
1.XAML 接触WPF的第一眼就是XAML---XAML是用来描绘界面的.其实不然! "Actually, XAML has nothing to do with UI. It's mer ...
- Call C# in powershell
How to call C# code in powershell Powershell Command Add-Type usage of Add-Type we use Add-Type -Typ ...
- Windows Phone 8 开发必备资源
一.MVVM框架推荐 1. MVVM-Light 这个框架是我最常用的MVVM框架之一,它比Prism更轻量级,但对于一般的小应用,功能足够. 官方网站:http://mvvmlight.codepl ...
- 字符串的驻留(String Interning)
http://www.cnblogs.com/artech/archive/2007/03/04/663728.html 关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大 ...
随机推荐
- 五、python使用模块
if __name__=='__main__':用法: 当我们在命令行运行模块文件时,Python解释器把一个特殊变量__name__置为__main__,而如果在其他地方导入该hello模块时,if ...
- 应用数据存储到sdcard上一定要规范,android4.4.2有新规范
如果你的android设备有内部存储空间,即通常所说的机身存储(这就是指主要外部存储),那么你从外部插入SD卡就是一个二级外部存储设备. 最新的Android 4.4系统中,外置存储卡(SD卡)被称为 ...
- Django settings — Django 1.6 documentation
Django settings - Django 1.6 documentation export DJANGO_SETTINGS_MODULE=mysite.settings django-admi ...
- Struts2类型转换器
概述 A .从一个HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串. –HTTP没有"类型" 的概念.每一项表单输入只可能是一个字符串或一个字符串数组. ...
- Gym 100507H Pair: normal and paranormal (贪心)
Pair: normal and paranormal 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/H Description ...
- nyoj 120 校园网络(求添加多少条边使整个图强连通)
校园网络 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 南阳理工学院共有M个系,分别编号1~M,其中各个系之间达成有一定的协议,如果某系有新软件可用时,该系将允许一 ...
- [1.1]Knowledge that should be prepared
Actually, there are a huge amount of knowledge we need to learn. So I hope you don't be scared. It's ...
- asp.net中使用swfupload上传大文件
转载:http://www.cnblogs.com/niunan/archive/2012/01/12/2320705.html 花了一天多时间研究出来的,其实也就是网上下别人的代码然后再自己修修改改 ...
- Meta键盘
由于著名的编辑器Emacs中用到Meta键,但如今大多国人所用键盘上实际并无此键,想必多有不明之处,故多方收集资料撰写此文,简要描述了Meta键及相关键盘的发展始末,至于在Emacs上如何使用国人键盘 ...
- 【转】python中的lambda函数
http://www.cnblogs.com/coderzh/archive/2010/04/30/python-cookbook-lambda.html lambda函数也叫匿名函数,即,函数没有具 ...