งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

การพัฒนา web crawler ขั้นพื้นฐาน ด้วย crawler4j

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "การพัฒนา web crawler ขั้นพื้นฐาน ด้วย crawler4j"— ใบสำเนางานนำเสนอ:

1 การพัฒนา web crawler ขั้นพื้นฐาน ด้วย crawler4j

2 เครื่องมือที่ใช้พัฒนา
Eclipse IDE Java SDK Library “crawler4j” download >> “ ”

3 การทำงานของ web crawler

4 การทำงานของ web crawler
ขั้นแรกจะเป็นกระบวนการที่ใช้ในการเก็บ URL ของเว็บเพจ (Intialize Frontier) ใช้การค้นหาข้อมูลแนวกว้าง (Breadth First Search) ใช้คิวแบบเข้าก่อนออกก่อน (First In First Out : FIFO) หาข้อมูลโดยใช้เส้นทางในการค้นหาที่สั้นที่สุด (Shortest Paths) ขั้นตอนในการดึง URL (Dequeue) ไปให้ส่วนที่ทำหน้าที่ร้องขอหรือ ดาวน์โหลดเว็บเพจ (Fetch Page)

5 การทำงานของ web crawler
ทำการสกัด URL (Extract URLs) จากเว็บเพจที่ถูกดึงมาแล้วว่ามี URL ที่ต้องทำงานต่ออีกหรือไม่ เป็นการหาความลึกของลิงค์ (Link Depth) หากมีจะทำการบันทึก URL นั้นลงในฐานข้อมูล วนไปเริ่มกระบวนการใหม่จนกว่าจะไม่มีลิงค์ URL จึงจะเสร็จสิ้น กระบวนการ

6 Step 1 Download the latest crawler4j-x.x.zip file (contains crawler4j-x.x.jar) zip file also contains a log4j.properties file that you'll need later to properly create a log4j appender. download >> “ ”

7 Step 2 In Eclipse, create a new project, such as WebcrawlerTest.
Right click on your new web crawler project and select the Build Path option. Select the “Add External Archives” option and select ALL of the jar files that you extracted from the zip files.

8 Step 3 Create a Java class name “BasicCrawler”
import edu.uci.ics.crawler4j.crawler.Page; import edu.uci.ics.crawler4j.crawler.WebCrawler; import edu.uci.ics.crawler4j.parser.HtmlParseData; import edu.uci.ics.crawler4j.url.WebURL; import java.util.Set; import java.util.regex.Pattern; import org.apache.http.Header; public class BasicCrawler extends WebCrawler { private final static Pattern FILTERS = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g" + "|png|tiff?|mid|mp2|mp3|mp4"+ "|wav|avi|mov|mpeg|ram|m4v|pdf" + "|rm|smil|wmv|swf|wma|zip|rar|gz))$"); }

9 Step 3 Create method name “shouldVisit”
Implement this function to specify whether the given url . It should be crawled or not (based on crawling logic). public boolean shouldVisit(Page page, WebURL url) {     String href = url.getURL().toLowerCase();     return !FILTERS.matcher(href).matches() && href.startsWith("   }

10 Step 3 Create method name “visit”
This function is called when a page is fetched and ready to be processed by your program. public void visit(Page page) {     int docid = page.getWebURL().getDocid();     String url = page.getWebURL().getURL();     String domain = page.getWebURL().getDomain();     String path = page.getWebURL().getPath();     String subDomain = page.getWebURL().getSubDomain();     String parentUrl = page.getWebURL().getParentUrl();     String anchor = page.getWebURL().getAnchor();  // continue next page >>>

11 Step 3 System.out.println("Docid: " + docid);     System.out.println("URL: " + url);     System.out.println("Domain: '" + domain + "'");     System.out.println("Sub-domain: '" + subDomain + "'");     System.out.println("Path: '" + path + "'");     System.out.println("Parent page: " + parentUrl);     System.out.println("Anchor text: " + anchor);     if (page.getParseData() instanceof HtmlParseData) {       HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();       String text = htmlParseData.getText();       String html = htmlParseData.getHtml();       List<WebURL> links = htmlParseData.getOutgoingUrls();       System.out.println("Text length: " + text.length());       System.out.println("Html length: " + html.length());       System.out.println("Number of outgoing links: " + links.size());     } System.out.println("============="); }    

12 Step 4 Create a Java class name “BasicCrawlController”
import edu.uci.ics.crawler4j.crawler.CrawlConfig; import edu.uci.ics.crawler4j.crawler.CrawlController; import edu.uci.ics.crawler4j.fetcher.PageFetcher; import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig; import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer; public class BasicCrawlController { }

13 Step 4 Create main method
public static void main(String[] args) throws Exception { String crawlStorageFolder = "C:\\Storage\\"; int numberOfCrawlers = 1     CrawlConfig config = new CrawlConfig();     config.setCrawlStorageFolder(crawlStorageFolder); config.setPolitenessDelay(1000); config.setMaxDepthOfCrawling(2); config.setMaxPagesToFetch(1000); config.setResumableCrawling(false); //continue next page

14 Step 4 PageFetcher pageFetcher = new PageFetcher(config);     RobotstxtConfig robotstxtConfig = new RobotstxtConfig();     RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);     CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer); controller.addSeed("     controller.start(BasicCrawler.class, numberOfCrawlers);   }

15 Step 5 Run Select Run → Run Configurations option and select your project and BasicCrawlController as the main class. Click Apply button and then Run to start your first webcrawler. Deleting content of: C:\Storage\frontier  INFO [main] Crawler 1 started.  INFO [main] Crawler 2 started.

16 Step 5 Result Docid: 1 URL: Domain: 'uci.edu' Sub-domain: ' Path: '/~lopes/' Parent page: null Anchor text: null Text length: 2442 Html length: 9987 Number of outgoing links: 34 =============

17 Step 5 Finish Crawl INFO [Thread-1] It looks like no thread is working, waiting for 10 seconds to make sure...  INFO [Thread-1] No thread is working and no more URLs are in queue waiting for another 10 seconds to make sure...  INFO [Thread-1] All of the crawlers are stopped. Finishing the process...  INFO [Thread-1] Waiting for 10 seconds before final clean up...


ดาวน์โหลด ppt การพัฒนา web crawler ขั้นพื้นฐาน ด้วย crawler4j

งานนำเสนอที่คล้ายกัน


Ads by Google