本套件為網路上常見的資料協定、格式,提供了簡易且方便(easy-to-use)的操作接口。套件主要以Jsoup為核心擴展,整合Apache Commons-VFS後,提供更多種協定的操作,也可支援壓縮格式處理。
Requires JDK 1.7 or higher
To add a dependency on CrawlerPack using Maven, use the following:
<dependency>
<groupId>com.github.abola</groupId>
<artifactId>crawler</artifactId>
<version>1.0.3</version>
</dependency>
To add a dependency using Gradle:
dependencies {
compile 'com.github.abola:crawler:1.0.3'
}
And easy-to-use example
// URI format source
String uri = "https://raw.githubusercontent.com/abola/CrawlerPack/master/test.json";
CrawlerPack.start()
.getFromJson(uri)
.select("results name").text() ;
使用 Apache Commons-VFS 所支援所有協定,常見網路協定如http/https,samba(cifs),ftp,sftp等…詳細列表請參考 https://commons.apache.org/proper/commons-vfs/filesystems.html
- JSON
- XML
- HTML
爬蟲包套件可正常的處理使用中文命名的XML或JSON
XML
<集合>
<元素>元素名稱1</元素>
<元素>元素名稱2</元素>
</集合>
JSON
{"集合":[
{"元素":"元素名稱1"}
, {"元素":"元素名稱2"}
]}
爬蟲包建議使用 UTF-8 操作資料。針對非 UTF-8 編碼的遠端資料,爬蟲包預設會啟動自動偵測編碼,並將其轉換為 UTF-8 編碼。
注意,預設啟用的自動編碼,效能會明顯的不如直接指定編碼,平均測試較直接指定編碼的目標多出300ms以上耗費時間。如果遠端資料非 UTF-8 編碼,大量資料擷取時,直接指定遠端編碼,可有效減少你作業整體耗時。
// TWSE 2015'三大法人買賣金額統計表
String uri = "http://www.twse.com.tw/ch/trading/fund/BFI82U/BFI82U_print.php"
+"?begin_date=20150101&end_date=20151231&report_type=month";
CrawlerPack.start()
.setRemoteEncoding("big5") // 直接指定遠端編碼
.getFromHtml(uri)
.select("table.board_trad > tbody > tr:nth-child(7) > td:nth-child(4)").text()
// 即時PM2.5資料
String uri = "http://opendata2.epa.gov.tw/AQX.json";
CrawlerPack.start()
.getFromJson(uri)
.getElementsByTag("pm2.5").text();
// 104司人力銀行上 10萬月薪以上的工作資料
String uri = "http://www.104.com.tw/i/apis/jobsearch.cfm?order=2&fmt=4&cols=JOB,NAME&slmin=100000&sltp=S&pgsz=20";
CrawlerPack.start()
.getFromXml(uri)
.select("item").get(0).attr("job") ;
// ptt 笨版最新文章列表
String uri = "https://www.ptt.cc/bbs/StupidClown/index.html";
CrawlerPack.start()
.getFromHtml(uri)
.select("div.title > a").text();
// ptt 八掛版創立首篇廢文標題
String uri = "https://www.ptt.cc/bbs/Gossiping/M.1119222611.A.7A9.html";
CrawlerPack.start()
.addCookie("over18","1") // 必需在 getFromXXX 前設定Cookie
.getFromHtml(uri)
.select("span:containsOwn(標題) + span:eq(1)").text();
// 北市Youbike資訊
String uri = "gz:https://tcgbusfs.blob.core.windows.net/blobyoubike/YouBikeTP.gz";
// 列出所有大安區內的租借站
CrawlerPack.start()
.getFromJson(uri)
.select("retVal > *:contains(大安區)")
爬蟲包的主要目標,是提供簡易入門的操作模式。然而爬蟲包的效能並不理想,主要原因是編碼偵測 ,為了降低預設操作難度,使用了 juniversalchardet 自動偵測遠端內容編碼。直接指定遠端編碼可跳過自動偵測,提升一點效能。如果遠端為UTF8編碼 ,便不需要再指定。
以台灣證交所網站為例,若不指定編碼時,平均約600ms完成
// TWSE 2015'三大法人買賣金額統計表
String uri = "http://www.twse.com.tw/ch/trading/fund/BFI82U/BFI82U_print.php"
+"?begin_date=20150101&end_date=20151231&report_type=month";
# Guava Stopwatch
Stopwatch timer = Stopwatch.createStarted();
CrawlerPack.start()
.getFromHtml(uri);
System.out.println( timer.stop().toString() );
// avg 600ms
指定遠端編碼為big5後,減少了一點時間,減少的時間,會與你的處理器效能有關
Stopwatch timer = Stopwatch.createStarted();
CrawlerPack.start()
.setRemoteEncoding("big5")
.getFromHtml(uri);
System.out.println( timer.stop().toString() );
// avg 480ms
簡址
- 修正(跳過) 壓縮格式無法取得字元長度的臭蟲
- 修正抓取含路徑的打包檔時會出現 NullPointerException 問題
- 修正自動編碼偵測造成資料清空的bug
- 調整 getFromHtml 改使用 Jsoup 內建 Html parser
- 增加自動編碼偵測功能 (add library juniversalchardet)
- 增加 setRemoteEncoding(String encoding) 設定遠端內文編碼
- 調整 api 操作界面
- 增加對Cookie的支援
- 修正解析註解以及 js 中特殊符號的錯誤
- 修正動態網頁資料被cache的問題
- 增加授權,使用Apache 2.0 公開授權
- 專案已上傳至公開的 Maven Repository 現在可以直接透過pom.xml使用爬蟲包
- 修正 https PKIX 驗證無法通過的問題
- JCConf 2015 TW 高效率資料爬蟲組合包 投影片 http://www.slideshare.net/ssuser438746/jcconf-2015-tw