-
大小: 2.25MB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2023-11-08
- 語言: Java
- 標(biāo)簽:
資源簡介
使用java代碼基于MyEclipse開發(fā)環(huán)境實(shí)現(xiàn)爬蟲抓取網(wǎng)頁中的表格數(shù)據(jù),將抓取到的數(shù)據(jù)在控制臺(tái)打印出來,需要后續(xù)處理的話可以在打印的地方對數(shù)據(jù)進(jìn)行操作。包解壓后導(dǎo)入MyEclipse就可以使用,在TestCrawTable中右鍵運(yùn)行就能看到抓取到的數(shù)據(jù)。其他的調(diào)試可以自行修改。

代碼片段和文件信息
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;
//?要抓取的表格可能出現(xiàn)的屬性名
//?static?String[]?Propertys={“企業(yè)名稱““注冊號/統(tǒng)一社會(huì)信用代碼““被列入經(jīng)營異常名錄日期“};
static?String[]?Propertys?=?{?“地塊編號“?“宗地編號“?“地塊位置“?“用地性質(zhì)“?“規(guī)劃面積“
“出讓面積“?“發(fā)布時(shí)間“?“掛牌起始價(jià)“?“位置“?“交易時(shí)間“?“面積“?“規(guī)劃用途“?“容積率“?“起價(jià)“
“成交價(jià)“?“交易方式“?“競得人“?};
//?取最里面的table進(jìn)入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? 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? 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? 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? newElement.appendChild(trs.get(i));
}
return?newElement;
}
return?element;
}
private?static?boolean?isValueElements(String[]?Propertys
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????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個(gè)文件信息
評論
共有 條評論