




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、河北師范大學(xué)軟件學(xué)院網(wǎng)絡(luò)爬蟲1. 實(shí)驗(yàn)?zāi)繕?biāo)1. 熟悉網(wǎng)絡(luò)爬蟲的相關(guān)概念及實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲的相關(guān)流程。2. 了解WebCollector框架的基本原理。3. 熟練掌握在Eclipse項(xiàng)目中配置使用WebCollector爬蟲。2. 前提條件1 正確安裝和配置Java開發(fā)環(huán)境。2 了解網(wǎng)絡(luò)爬蟲的相關(guān)知識3 進(jìn)入WebCollector官方網(wǎng)站下載所需jar包。3. 實(shí)驗(yàn)任務(wù)及完成標(biāo)準(zhǔn)本次實(shí)驗(yàn)通過WebCollector框架實(shí)現(xiàn)一個簡單的聚焦網(wǎng)絡(luò)爬蟲。用戶可根據(jù)自己的需求定制網(wǎng)絡(luò)爬蟲,設(shè)定待爬取的網(wǎng)址、爬取網(wǎng)頁的數(shù)量、爬取網(wǎng)頁的內(nèi)容等。通過對該實(shí)例的詳細(xì)介紹來探討網(wǎng)絡(luò)爬蟲的原理及在實(shí)際生活中的應(yīng)用。在此
2、實(shí)例的基礎(chǔ)上,學(xué)生需要獨(dú)立完成更為復(fù)雜的聚焦網(wǎng)絡(luò)爬蟲,來爬取更有意義的內(nèi)容。具體要求見“4 擴(kuò)展內(nèi)容”。3.1 建立應(yīng)用程序項(xiàng)目打開eclipse,創(chuàng)建本次實(shí)驗(yàn)項(xiàng)目htmlCrawler(【File】-【New】-【Java Project】)如圖1所示。圖1 創(chuàng)建工程3.2 在Eclipse項(xiàng)目中配置使用WebCollector爬蟲1 選中 htmlCrawler 右鍵,選中【New】-【Folder】,輸入文件名稱“l(fā)ib”,如下圖2所示。圖2 創(chuàng)建文件夾2解壓縮 webcollector-2.26-bin.zip,拷貝所有的Jar包 ,放到lib目錄中,如圖3所示。圖3 目錄結(jié)構(gòu)圖3將文
3、件夾lib中的所有jar包添加到build path中,如圖4、圖5、圖6所示。圖4 圖5 圖6依次選中jar包,導(dǎo)入到工程中。4 如果想看WebCollector的源碼,可以為Jar包關(guān)聯(lián)源碼(可選),如圖6、圖7所示。圖6圖73.3 現(xiàn)在可以編寫WebCollector爬蟲的代碼了新建一個類Crawler.java,繼承自BreadthCrawler。重寫visit方法,來實(shí)現(xiàn)滿足自己需求的爬蟲即可。如圖8、圖9所示。圖8圖93.4 抓取河北師大軟件學(xué)院網(wǎng)站首頁的源代碼package htmlCrawler;import java.io.IOException;import .
4、hfut.dmic.webcollector.model.CrawlDatums;import .hfut.dmic.webcollector.model.Page;import .hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;import .hfut.dmic.webcollector.util.FileUtils;public class Crawler extends BreadthCrawler public Crawler(String crawlPath, boolean autoPa
5、rse) super(crawlPath, autoParse);/ TODO Auto-generated constructor stubOverridepublic void visit(Page page, CrawlDatums next) / TODO Auto-generated method stubtry System.out.println(正在提?。?page.getUrl();/將爬取的內(nèi)容寫到test.html頁FileUtils.writeFileWithParent(downloads/test.html, page.getContent(); catch (IO
6、Exception e) / TODO Auto-generated catch blocke.printStackTrace(); public static void main(String args) /創(chuàng)建爬蟲對象 Crawler crawler = new Crawler(html_crawler,true); crawler.addSeed( try /啟動 爬蟲crawler.start(1); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); 控制臺輸出結(jié)果:2016-02-25
7、14:23:06 INFO .hfut.dmic.webcollector.crawler.Crawler - start depth 12016-02-25 14:23:06 INFO .hfut.dmic.webcollector.fetcher.Fetcher - open generator:.hfut.dmic.webcollector.plugin.berkeley.BerkeleyGenerator2016-02-25 14:23:06 INFO .hfut.dmic.webcollector.fetcher.Fetcher - i
8、nit segmentWriter:.hfut.dmic.webcollector.plugin.berkeley.BerkeleyDBManager2016-02-25 14:23:07 INFO .hfut.dmic.webcollector.fetcher.Fetcher - -activeThreads=1, spinWaiting=0, fetchQueue.size=02016-02-25 14:23:07 INFO .hfut.dmic.webcollector.fetcher.Fetcher - fetch URL: 正在提?。?016-02
9、-25 14:23:08 INFO .hfut.dmic.webcollector.fetcher.Fetcher - -activeThreads=0, spinWaiting=0, fetchQueue.size=02016-02-25 14:23:08 INFO .hfut.dmic.webcollector.fetcher.Fetcher - clear all activeThread2016-02-25 14:23:08 INFO .hfut.dmic.webcollector.fetcher.Fetcher - close generator:
10、.hfut.dmic.webcollector.plugin.berkeley.BerkeleyGenerator2016-02-25 14:23:08 INFO .hfut.dmic.webcollector.fetcher.Fetcher - close segmentwriter:.hfut.dmic.webcollector.plugin.berkeley.BerkeleyDBManager2016-02-25 14:23:08 INFO .hfut.dmic.webcollector.plugin.berkeley.BerkeleyDB
11、Manager - start merge2016-02-25 14:23:08 INFO .hfut.dmic.webcollector.plugin.berkeley.BerkeleyDBManager - merge fetch database2016-02-25 14:23:08 INFO .hfut.dmic.webcollector.plugin.berkeley.BerkeleyDBManager - merge link database2016-02-25 14:23:08 INFO .hfut.dmic.webcollector.plu
12、gin.berkeley.BerkeleyDBManager - end merge2016-02-25 14:23:08 INFO .hfut.dmic.webcollector.crawler.Crawler - depth 1 finish: TOTAL urls:1TOTAL time:2 seconds爬取結(jié)果如圖10所示。圖104 擴(kuò)展內(nèi)容4.1 在本實(shí)驗(yàn)的基礎(chǔ)上,進(jìn)行相關(guān)的設(shè)置:(1)爬取的深度;設(shè)置開啟的線程數(shù);(2)設(shè)置爬取url的上限;(3)通過正則表達(dá)式設(shè)置爬取哪些網(wǎng)頁,不爬取哪些網(wǎng)頁等(eg./*不要爬取jpg|png|gif*/ ,代碼:crawler.a
13、ddRegex(-.*.(jpg|png|gif).*);)。(4)實(shí)現(xiàn)代碼由學(xué)生自己完成4.2 實(shí)現(xiàn)自己的網(wǎng)絡(luò)爬蟲,抓取“新浪新聞”(1)需要抓取信息包括:網(wǎng)址,標(biāo)題,時間,網(wǎng)頁內(nèi)容等。(2)將抓取的數(shù)據(jù)寫到文本文件或excel表中。(3)抓取代碼由學(xué)生自己完成。提示:將數(shù)據(jù)寫到excel表中,需要導(dǎo)入poi-3.14-beta1-20151223.jar包,部分代碼如下:/創(chuàng)建一個EXCEL Workbook wb = new HSSFWorkbook();./創(chuàng)建一個SHEET sheet1 = wb.createSheet(數(shù)據(jù)解析); String title = url,標(biāo)題,評論
14、,時間,內(nèi)容,本地相對路徑; int i=0; /創(chuàng)建一行 Row row = sheet1.createRow(short)0); /填充標(biāo)題 for (String s:title) Cell cell = row.createCell(i); cell.setCellValue(s); i+; /下面是填充數(shù)據(jù)的部分代碼AtomicInteger id=new AtomicInteger(0);Row row = sheet1.createRow(short)id.incrementAndGet(); row.createCell(0).setCellValue(url); row.createCell(1).setCellValue(title); row.createCell(2).setCellValue(comment);row.createCell(3).setCellValue(time);row.createCell(4).setCellValue(contents);row.createCell(5).setCellValue(path);4.3 改進(jìn)4.2中自己的網(wǎng)絡(luò)爬蟲,將抓取的數(shù)據(jù)直接寫到數(shù)據(jù)庫中。實(shí)現(xiàn)代碼由學(xué)生自己完成。提示:使用JDBC連接數(shù)據(jù)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025西交利物浦大學(xué)輔導(dǎo)員考試試題及答案
- 2025遼寧兵器工業(yè)職工大學(xué)輔導(dǎo)員考試試題及答案
- 2025濰坊科技學(xué)院輔導(dǎo)員考試試題及答案
- 新生兒貧血護(hù)理要點(diǎn)解析
- 畢業(yè)設(shè)計(jì)實(shí)訓(xùn)答辯
- T/ZBH 022-2023薄膜太陽能電池用FTO導(dǎo)電玻璃
- 河南投資集團(tuán)工程管理有限公司招聘筆試題庫2025
- 小學(xué)生健康衛(wèi)生知識講座
- 2025年職業(yè)能力評估與發(fā)展考試試題及答案
- 2025年現(xiàn)代漢語基礎(chǔ)知識考試卷及答案
- 肺炎住院病歷及病程記錄教學(xué)文案
- 檢察院書記員考試試題法院書記員考試試題
- 金風(fēng)科技5MW風(fēng)力發(fā)電機(jī)專業(yè)題庫分解
- 排球比賽計(jì)分表2
- 水中樁、水上平臺施工專項(xiàng)方案
- 儀器設(shè)備管理培訓(xùn)課件(共88頁).ppt
- 食堂食品定點(diǎn)采購詢價記錄表
- Fuji Flexa程序制作步驟
- 深國交數(shù)學(xué)模擬試題1
- ICOM 2720中文說明書
- 關(guān)于琿春市水產(chǎn)業(yè)發(fā)展情況的調(diào)研報(bào)告
評論
0/150
提交評論