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 关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大 ...
随机推荐
- mongoDB 3.0 安全权限访问控制 -摘自网络
"E:\Program Files\MongoDB\Server\3.0\bin\mongod.exe" --logpath E:\mongodb\log\mongodblog.l ...
- ES6学习小计
1.增加了for of语法,对应C#里的foreach,注意ES5中的 for in只会传递0,1,2.....序号,并且是字符for-of循环语句通过方法调用来遍历各种集合.数组.Maps对象.Se ...
- CoffeeScript学习(2)—— 变量
变量基础 对于变量的定义的话,形式如下所示 xxx = yyy ------编译后------ var xxx = yyy; 保留字 我们知道,在原生js中的保留字是不能作为变量名或者属性名的.如果我 ...
- POJ 3660 Cow Contest(传递闭包floyed算法)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5989 Accepted: 3234 Descr ...
- mongoDB在windows下基于配置文件的安装和权限配置方式
下载mongoDB http://www.mongodb.org/downloads 根据操作系统,选择需要下载的安装包 添加mongodb 安装目录 将解压的文件夹中内容拷贝,存放在想要安装的文件 ...
- HDU 2275 multiset
题意:n个操作 Push 入容器 Pop弹出一个 满足<=该数的最大的数(若没有输出No Element!) 开始用set打了一遍wrong了,这里入容器的数是有重复的,所以用multiset ...
- this指针和m_hWnd的区别
m_hWnd ① m_hWnd这个成员变量,最早是定义在类CWnd中,而且是类CWnd的第一个数据成员, 先看一下MSDN的解析: The handle of the Windows window a ...
- linux 下安装apache 快速教程
最近自学linux,看鸟哥的文章.提到了apache,所以在虚拟机redhat 5下安装了一把, 结合国内外文章写下快速可行的教程: --------------------------------- ...
- node-webkit
最近迷上了node-webkit工程.准备搜集并整理一些东西放在这里
- 在Oracle数据库启动时提示没有权限 ora-01031:insufficient privileges
环境:Linux 操作语句: [oracle@ora11r2 ~]$ sqlplus /nolog SQL*Plus: Release 11.1.0.6.0 - Production on Thu J ...