上篇文章,介绍了Heritrix爬取土木在线网的一些html数据,今天介绍如何将这些数据导入数据库。
首先建立一个web工程,写好domain,这是javaweb的基础,不在过多介绍。
package cn.hpu.edu.heritrix.domain;
public class Extractor_TuMu {
private String id;
private String title;
private String nowdate;
private String related;//分类
private String rate ;//评分
private int price;//金额
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNowdate() {
return nowdate;
}
public void setNowdate(String nowdate) {
this.nowdate = nowdate;
}
public String getRelated() {
return related;
}
public void setRelated(String related) {
this.related = related;
}
public String getRate() {
return rate;
}
public void setRate(String rate) {
this.rate = rate;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
建立实体类后,配置数据库的相关信息,这里用的是c3p0连接池,不太懂的同学,可以按照自己的方法,链接数据库。接下来我也会陆陆续续,将javaweb的知识写下来,与大家共享。想使用c3p0的同学,可以参考我后续的博客,这里省略。
直接看Dao的操作类:
package cn.hpu.edu.heritrix.dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import cn.hpu.edu.heritrix.domain.Extractor_TuMu;
import cn.itcast.commons.CommonUtils;
import cn.itcast.jdbc.TxQueryRunner;
public class TuMuDao {
private QueryRunner qr = new TxQueryRunner();
/**
* 向数据库添加数据
* @param e
* @throws SQLException
*/
public void add(Extractor_TuMu e) throws SQLException{
String sql = "insert into t_tumu values(?,?,?,?,?,?)";
Object[] params = {e.getId(),e.getTitle(),e.getNowdate(),e.getRelated(),e.getRate(),e.getPrice()};
qr.update(sql,params);
}
}
这些准备工作完毕后,就是对网页数据的爬取了,现将代码粘出来,略作解释:
从main方法说起:首先要将保存目录获取到,之后对该目录进行遍历,得到所有的文件名。package cn.hpu.edu.heritrix.extractor; import java.io.File; import java.sql.SQLException; import org.htmlparser.Node; import org.htmlparser.Parser; import org.htmlparser.filters.AndFilter; import org.htmlparser.filters.HasAttributeFilter; import org.htmlparser.filters.NodeClassFilter; import org.htmlparser.nodes.TextNode; import org.htmlparser.tags.Div; import org.htmlparser.tags.LinkTag; import org.htmlparser.tags.ParagraphTag; import org.htmlparser.tags.Span; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; import cn.hpu.edu.heritrix.dao.TuMuDao; import cn.hpu.edu.heritrix.domain.Extractor_TuMu; import cn.itcast.commons.CommonUtils; public class Extractor { private TuMuDao tu = new TuMuDao(); /** *为
标签重新建立解析器 * @param url * @return * @throws ParserException */ private static NodeList mothed(String url) throws ParserException{ Parser parser = new Parser(url); AndFilter andFilter = new AndFilter(new NodeClassFilter(ParagraphTag.class), new HasAttributeFilter("class","about clearfix")); NodeList nodeList = parser.parse(andFilter); //System.out.println(nodeList.elementAt(0)); return nodeList; } /** * 解析DIV并保存到数据库 * @param url * @throws ParserException * @throws SQLException */ public void extractor_TuMu(String url) throws ParserException, SQLException{ Parser parser = new Parser(url); AndFilter andFilter = new AndFilter(new NodeClassFilter(Div.class), new HasAttributeFilter("class","li_con fl")); NodeList nodeList = parser.parse(andFilter); NodeList nodeListone = mothed(url); int size = nodeList.size(); for(int i=0; i
标签中获取node Node nodeone = nodeListone.elementAt(i); // 标签中第一个元素,Span Span span = (Span) nodeone.getChildren().elementAt(0); //从Span中得到评分 TextNode rateNode = (TextNode) span.getChild(2); e.setRate(rateNode.getText()); //
标签中第二个元素,Span Span spantwo = (Span) nodeone.getChildren().elementAt(1); TextNode priceNode = (TextNode) spantwo.getChild(2); e.setPrice(Integer.parseInt(priceNode.getText())); //向数据库添加数据 e.setId(CommonUtils.uuid()); tu.add(e); } } public static void main(String[] args) { //拿到文件目录 File files = new File("H:\\Space\\myHeriterix\\jobs\\Tumu-20151202023408932\\mirror\\ziliao.co188.com\\drawing9258\\"); File[] list = files.listFiles(); //遍历文件夹 for(File file : list){ Extractor e = new Extractor(); try { e.extractor_TuMu("H:\\Space\\myHeriterix\\jobs\\Tumu-20151202023408932\\mirror\\ziliao.co188.com\\drawing9258\\"+file.getName()); } catch (ParserException e1) { e1.printStackTrace(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }
这里是结合HttpParser对数据进行过滤。而Parser可以根据本地的目录对网页解析,亦可以跟据一个网页的URL对其进行解析。实例化Parser之后,建立一个AndFilter对标签进行解析。由于这里需要的数据不再同一个标签内,所以需要两个AndFilter解析,但是一个Parser只能解析一个AndFilter,故又写了一个方法,重新构建了一个Parser对象,对两一个标签解析。
解析出来之后,将结果放到一个NodeList中。再遍历该集合,就可以得到集合里面的节点,以及节点下面的子字节,子节点的文本信息以及各项信息。
LinkTag:
ParagraphTag:
Div:
Span :
TextNode :