资源简介
使用java代码基于MyEclipse开发环境实现爬虫抓取网页中的表格数据,将抓取到的数据在控制台打印出来,需要后续处理的话可以在打印的地方对数据进行操作。包解压后导入MyEclipse就可以使用,在TestCrawTable中右键运行就能看到抓取到的数据。其他的调试可以自行修改。
代码片段和文件信息
package com.panchong.test;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;
public class DataTableUtil {
private final static int NUM = 5;
// 要抓取的表格可能出现的属性名
// static String[] Propertys={“企业名称““注册号/统一社会信用代码““被列入经营异常名录日期“};
static String[] Propertys = { “地块编号“ “宗地编号“ “地块位置“ “用地性质“ “规划面积“
“出让面积“ “发布时间“ “挂牌起始价“ “位置“ “交易时间“ “面积“ “规划用途“ “容积率“ “起价“
“成交价“ “交易方式“ “竞得人“ };
// 取最里面的table进入isValueElement方法检测是不是我们需要的table
public static List getFitElement(Document document) {
if (Propertys != null) {
Element element = document.getElementsByTag(“body“).get(0);
List fitElments = new ArrayList();
Elements tableElements = element.getElementsByTag(“table“);
if (tableElements != null && tableElements.size() > 0) {
for (int i = 0; i < tableElements.size(); i++) {
Element tableElement = tableElements.get(i);
Elements ces = tableElement.getElementsByTag(“table“);
if (ces != null && ces.size() > 1) {
} else {
TableElement te;
if ((te = isValueElement(Propertys tableElement)) != null) {
fitElments.add(te);
}
}
}
} else {
return null;
}
return fitElments;
}
return null;
}
private static Element removeRedundance(String[] Propertys Element element) {
Elements tres = element.getElementsByTag(“tr“);
Element trElement = tres.get(0);
Elements tde = trElement.getElementsByTag(“td“);
int row = 1;
for (Element tdElement : tde) {
String attribute = tdElement.attr(“rowspan“);
if (attribute != null && !attribute.equals(““)) {
int rowSpan = Integer.valueOf(attribute);
if (rowSpan > row) {
row = rowSpan;
}
}
}
List elements = new ArrayList();
for (int i = 0; i < row; i++) {
elements.add(tres.get(i));
}
int ri = 0;
while (!isValueElements(Propertys elements)) {
elements = new ArrayList();
row = 1;
Elements tdes = tres.get(ri).getElementsByTag(“td“);
for (Element tdElement : tdes) {
String attribute = tdElement.attr(“rowspan“);
if (attribute != null && !attribute.equals(““)) {
int rowSpan = Integer.valueOf(attribute);
if (rowSpan > row) {
row = rowSpan;
}
}
}
for (int i = 0; i < row; i++) {
elements.add(tres.get(ri + i));
}
ri = ri + row;
}
if (ri > 0) {
Elements trs = element.getElementsByTag(“tr“);
int size = trs.size();
Element newElement = new Element(Tag.valueOf(“table“) “table“);
for (int i = ri - row; i < size; i++) {
newElement.appendChild(trs.get(i));
}
return newElement;
}
return element;
}
private static boolean isValueElements(String[] Propertys
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 885 2016-12-09 17:47 PanChongTest\.classpath
文件 388 2016-12-09 17:15 PanChongTest\.project
文件 598 2016-12-09 17:15 PanChongTest\.settings\org.eclipse.jdt.core.prefs
文件 36586 2016-12-09 17:16 PanChongTest\bin\chardet.jar
文件 7712 2016-12-15 15:28 PanChongTest\bin\com\panchong\test\DataTableUtil.class
文件 732 2016-12-09 17:47 PanChongTest\bin\com\panchong\test\PropertyInfo.class
文件 1548 2016-12-09 17:47 PanChongTest\bin\com\panchong\test\SimFeatureUtil.class
文件 1150 2016-12-09 17:47 PanChongTest\bin\com\panchong\test\StringUtil.class
文件 991 2016-12-09 17:47 PanChongTest\bin\com\panchong\test\TableElement.class
文件 8053 2016-12-09 17:47 PanChongTest\bin\com\panchong\test\TableUtil.class
文件 2277 2016-12-15 15:26 PanChongTest\bin\com\panchong\test\TestCrawTable.class
文件 207723 2016-12-09 17:16 PanChongTest\bin\commons-lang.jar
文件 55424 2016-12-09 17:16 PanChongTest\bin\commons-logging.jar
文件 51526 2016-12-09 17:16 PanChongTest\bin\cpdetector.jar
文件 433368 2016-12-09 17:16 PanChongTest\bin\httpclient-4.2.5.jar
文件 227275 2016-12-09 17:16 PanChongTest\bin\httpcore-4.2.4.jar
文件 293672 2016-12-09 17:16 PanChongTest\bin\jsoup-1.7.2.jar
文件 36586 2016-12-09 17:16 PanChongTest\lib\chardet.jar
文件 207723 2016-12-09 17:16 PanChongTest\lib\commons-lang.jar
文件 55424 2016-12-09 17:16 PanChongTest\lib\commons-logging.jar
文件 51526 2016-12-09 17:16 PanChongTest\lib\cpdetector.jar
文件 433368 2016-12-09 17:16 PanChongTest\lib\httpclient-4.2.5.jar
文件 227275 2016-12-09 17:16 PanChongTest\lib\httpcore-4.2.4.jar
文件 293672 2016-12-09 17:16 PanChongTest\lib\jsoup-1.7.2.jar
文件 6993 2016-12-15 15:28 PanChongTest\src\com\panchong\test\DataTableUtil.java
文件 341 2016-12-09 17:19 PanChongTest\src\com\panchong\test\PropertyInfo.java
文件 1370 2016-12-09 17:36 PanChongTest\src\com\panchong\test\SimFeatureUtil.java
文件 451 2016-12-09 17:18 PanChongTest\src\com\panchong\test\StringUtil.java
文件 608 2016-12-09 17:17 PanChongTest\src\com\panchong\test\TableElement.java
文件 8531 2016-12-09 17:37 PanChongTest\src\com\panchong\test\TableUtil.java
............此处省略15个文件信息
评论
共有 条评论