




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、河北師范大學(xué)軟件學(xué)院網(wǎng)絡(luò)爬蟲1. 實驗?zāi)繕?biāo)1. 熟悉網(wǎng)絡(luò)爬蟲的相關(guān)概念及實現(xiàn)網(wǎng)絡(luò)爬蟲的相關(guān)流程。2. 了解WebCollector框架的基本原理。3. 熟練掌握在Eclipse項目中配置使用WebCollector爬蟲。2. 前提條件1 正確安裝和配置Java開發(fā)環(huán)境。2 了解網(wǎng)絡(luò)爬蟲的相關(guān)知識3 進(jìn)入WebCollector官方網(wǎng)站下載所需jar包。3. 實驗任務(wù)及完成標(biāo)準(zhǔn)本次實驗通過WebCollector框架實現(xiàn)一個簡單的聚焦網(wǎng)絡(luò)爬蟲。用戶可根據(jù)自己的需求定制網(wǎng)絡(luò)爬蟲,設(shè)定待爬取的網(wǎng)址、爬取網(wǎng)頁的數(shù)量、爬取網(wǎng)頁的內(nèi)容等。通過對該實例的詳細(xì)介紹來探討網(wǎng)絡(luò)爬蟲的原理及在實際生活中的應(yīng)用。在此
2、實例的基礎(chǔ)上,學(xué)生需要獨立完成更為復(fù)雜的聚焦網(wǎng)絡(luò)爬蟲,來爬取更有意義的內(nèi)容。具體要求見“4 擴(kuò)展內(nèi)容”。3.1 建立應(yīng)用程序項目打開eclipse,創(chuàng)建本次實驗項目htmlCrawler(【File】->【New】->【Java Project】)如圖1所示。圖1 創(chuàng)建工程3.2 在Eclipse項目中配置使用WebCollector爬蟲1 選中 htmlCrawler 右鍵,選中【New】->【Folder】,輸入文件名稱“l(fā)ib”,如下圖2所示。圖2 創(chuàng)建文件夾2解壓縮 webcollector-2.26-bin.zip,拷貝所有的Jar包 ,放到lib目錄中,如圖3所示
3、。圖3 目錄結(jié)構(gòu)圖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方法,來實現(xiàn)滿足自己需求的爬蟲即可。如圖8、圖9所示。圖8圖93.4 抓取河北師大軟件學(xué)院網(wǎng)站首頁的源代碼package htmlCrawler;import java.io.IOException;im
4、port .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, bo
5、olean autoParse) super(crawlPath, autoParse);/ TODO Auto-generated constructor stubOverridepublic void visit(Page page, CrawlDatums next) / TODO Auto-generated method stubtry System.out.println("正在提?。?quot;+page.getUrl();/將爬取的內(nèi)容寫到test.html頁FileUtils.writeFileWithParent("downloads/test.html
6、", page.getContent(); catch (IOException 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-generat
7、ed catch blocke.printStackTrace(); 控制臺輸出結(jié)果:2016-02-25 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
8、INFO .hfut.dmic.webcollector.fetcher.Fetcher - init 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.
9、webcollector.fetcher.Fetcher - fetch URL: 正在提?。?016-02-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 .hfu
10、t.dmic.webcollector.fetcher.Fetcher - close generator:.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 cn
11、.edu.hfut.dmic.webcollector.plugin.berkeley.BerkeleyDBManager - 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
12、-02-25 14:23:08 INFO .hfut.dmic.webcollector.plugin.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 在本實驗的基礎(chǔ)上,進(jìn)行相關(guān)的設(shè)置:(1)爬取的深度;設(shè)置開啟的線程數(shù);(2)設(shè)置爬取url的上限;(3)通過正則表達(dá)式
13、設(shè)置爬取哪些網(wǎng)頁,不爬取哪些網(wǎng)頁等(eg./*不要爬取jpg|png|gif*/ ,代碼:crawler.addRegex("-.*.(jpg|png|gif).*");)。(4)實現(xiàn)代碼由學(xué)生自己完成4.2 實現(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();.
14、/創(chuàng)建一個SHEET sheet1 = wb.createSheet("數(shù)據(jù)解析"); String title = "url","標(biāo)題","評論","時間","內(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+;
15、/下面是填充數(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ù)庫中。實現(xiàn)代碼由學(xué)生自己完成。提示:使用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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 江西省宜春重點中學(xué)2022-2023學(xué)年聯(lián)考高二下學(xué)期語文期末試卷(含答案)
- 北京市西城區(qū)2023-2024學(xué)年五年級下學(xué)期語文期末試卷(含答案)
- 2025護(hù)工與老年人直接雇傭合同
- 2025合同法制的創(chuàng)新與發(fā)展趨勢
- 2025中介租賃合同書范本
- 2025年科技創(chuàng)業(yè)前如何精準(zhǔn)簽訂技術(shù)轉(zhuǎn)讓合同
- 2025年深圳市租房租賃合同簡易模板
- 2025年合作伙伴間的合同范本
- 2025鋁材買賣合同模板范本
- 《中國股市發(fā)展歷程》課件
- 2025屆上海市浦東新區(qū)高三二模英語試卷(含答案)
- 開曼群島公司法2024版中文譯本(含2024年修訂主要內(nèi)容)
- 【MOOC】航空燃?xì)鉁u輪發(fā)動機(jī)結(jié)構(gòu)設(shè)計-北京航空航天大學(xué) 中國大學(xué)慕課MOOC答案
- 悅己人生-大學(xué)生心理健康智慧樹知到期末考試答案章節(jié)答案2024年哈爾濱工業(yè)大學(xué)
- 職業(yè)衛(wèi)生評價考試計算題匯總
- JJF 1318-2011 影像測量儀校準(zhǔn)規(guī)范-(高清現(xiàn)行)
- 沉井工程施工綜合方案
- 產(chǎn)品開發(fā)控制程序
- 沖壓作業(yè)指導(dǎo)書模板
- MAPGIS使用手冊
- 員工工作調(diào)動單
評論
0/150
提交評論