Java 爬蟲框架,WebMagic 0.4.1 發布
此次更新加強了Ajax抓取的功能,并進行了一些功能改進。同時引入了重要的腳本化功能"webmagic-script",為今后的WebMagic-Avalon計劃做準備。
功能增強:
- 修復了抓取完頁面后,Spider偶爾無法退出的問題。詳細問題的分析,有興趣的可以點這里查看。
- 將抽取正文的SmartContentSelector中的算法改為哈工大的正文抽取算法https://code.google.com/p/cx-extractor/ ,經過測試,有較好的效果。 使用方法:Html.getSmartContent()。
- 為Page加入了更多的Http信息,包括http狀態碼"Page.getStatusCode()"和未解析過的正文"Page.getRawText()"。
- 為Spider增加一些監控信息,包括抓取的頁面數"Spider.getPageCount()",運行狀態"Spider.getStatus()"和執行線程數"Spider.getThreadAlive()"。
Ajax方面,在注解模式,引入了JsonPath表達式來進行抽取,示例代碼:
public class AppStore { @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..trackName") private String trackName; @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..description") private String description; @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..userRatingCount") private int userRatingCount; @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..screenshotUrls",multi = true) private List<String> screenshotUrls; public static void main(String[] args) { AppStore appStore = OOSpider.create(Site.me(), AppStore.class).<AppStore>get("http://itunes.apple.com/lookup?id=653350791&country=cn&entity=software"); System.out.println(appStore.trackName); System.out.println(appStore.description); System.out.println(appStore.userRatingCount); System.out.println(appStore.screenshotUrls); } }
JsonPath表達式的含義及具體用法看這里:http://www.oschina.net/p/jsonpath
WebMagic今后的目標是一個完整的產品,讓即使不會編碼的人也能通過簡單腳本,完成基本的爬蟲開發,并促進腳本分享。這就是WebMagic-Avalon計劃。大家可以查看https://github.com/code4craft/webmagic/issues/43 進行功能討論,歡迎各種建議。
目前第一期是要做到腳本化,對應文檔:
https://github.com/code4craft/webmagic/tree/master/webmagic-scripts
webmagic郵件組:https://groups.google.com/forum/#!forum/webmagic-java
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!