簡介
金色數據采集器開源項目,是用于抓取網頁和抽取數據的工具。其核心代碼是從金色數據抓取與數據融合平臺分離而來。
該項目提供抓取和抽取來自網頁數據,不僅可以抽取網頁內的內容,還能抽取URL、HTTP報頭、Cookie里的數據。
該項目定義了一種簡潔、靈活、敏捷的結構或者說是規則語法。極盡其所能將網頁內容、HTTP報頭、Cookie、甚至關聯其它網頁、其它網站數據,抽取出有意義有價值數據字段,組成一條數據記錄。除此之外,還能內嵌http請求,以補充數據字段,比如某些字段需要向詞典提供翻譯這樣的字段等等。
該項目還可支持從各種類型文檔抽取數據,比如html/xml/json/javascript/text等。
金色數據平臺
金色數據是集采集與融合功能的平臺,可將數據與數據之間的關系抓取下來并能立即融合到關聯關系應用數據庫表當中,并且反復采集融合也不會有重復數據產生。
完全免費的金色數據社區版
金色數據平臺社區版是一款抓取完全不受限(比如不限爬蟲數量、抓取速度/時間、數據抓取數量、導出數據數量)的私有云軟件,
我們提供了詳細的金色數據平臺免費文檔和培訓視頻,請點擊這里查看和使用。
使用入門
首先,我們需要將依賴加入項目當中即可(因為我們已經將該項目加入到maven中央倉庫),如下:
1、對于maven項目
<dependency> <groupId>com.100shouhou.golddata</groupId> <artifactId>golddata-spider</artifactId> <version>1.1.3</version> </dependency>
2、對于gradle項目
compile group: 'com.100shouhou.golddata', name: 'golddata-spider', version: '1.1.3'
然后你將可以使用該依賴所提供的簡潔清晰的API,如下:
@Test public void testGoldSpider(){ String ruleContent= " { \n"+ " __node: li.sky.skyid \n"+ " date: \n"+ " { \n"+ " expr: h1 \n"+ " __label: 日期 \n"+ " } \n"+ " sn: \n"+ " { \n"+ " \n"+ " js: md5(baseUri+item.date+headers['Content-Type']);\n"+ " } \n"+ " weather: \n"+ " { \n"+ " expr: p.wea \n"+ " } \n"+ " temprature: \n"+ " { \n"+ " expr: p.tem>i \n"+ " } \n"+ " } \n"; GoldSpider spider= com.xst.golddata.GoldSpider.newSpider() .setUrl("http://www.weather.com.cn/weather/101020100.shtml") .setRule(ruleContent) .request(); List list=spider.extractList(); // List<Weather> weathers=spider.extractList(Weather.class); // Weather weathers=spider.extractFirst(Weather.class); list.forEach( System.out::println); }
運行上面的測試,你將可以看類似下面的輸出:
{date=19日(今天), weather=陰轉小雨, temprature=10℃, sn=8bc265cb2bf23b6764b75144b255d81d} {date=20日(明天), weather=小雨轉多云, temprature=11℃, sn=9efd7e7bbbfb9bb06e04c0c990568bfd} {date=21日(后天), weather=多云轉中雨, temprature=11℃, sn=728539ac882721187741708860324afa} {date=22日(周六), weather=小雨, temprature=9℃, sn=a23fa2233e750a3bdd11b2e200ed06c3} {date=23日(周日), weather=小雨轉多云, temprature=8℃, sn=b27e1b8a8e92a7bed384ceb3e4fdfb5f} {date=24日(周一), weather=多云轉小雨, temprature=8℃, sn=c142b7fd12330ca031dd96b307c0d50d} {date=25日(周二), weather=小雨轉中雨, temprature=6℃, sn=16f71d3c8f09394588532a3ed1a8bacf}
當作Service或者API使用
你可以在項目中,可以當作調用服務和API使用。例如如下:
@Service public class WeatherServiceImpl implements WeatherService{ public List<Weather> listByCityId(Long cityId){ String url="http://www.weather.com.cn/weather/"+cityId+".shtml" String rule="<同上>" GoldSpider spider= com.xst.golddata.GoldSpider.newSpider() .setUrl(url) .setRule(ruleContent) .request(); return spider.extractList(Weather.class); } }
另外,你可以免費使用可視化編輯器來編輯規則內容, 可視化編輯器來自金色數據平臺,可以點擊鏈接下載,可視化編輯器截屏如下:
文檔和培訓視頻
規則內容以及文檔請點擊此處查看
你可以查看oschina的與金色數據相關博文, https://my.oschina.net/u/4095696?tab=newest