Crawler 4 j+jsoupに基づいて爬虫類を実現します。
4283 ワード
爬虫枠の分類
1.分布型爬虫類Nutch 2.Java単独機爬虫類Crawler 4 j、WebMagic、WebCollect 3.Java単独機爬虫類Scrrapyではない
開発の考え方
業務ニーズに応じて適切な爬虫枠を選択し、ウェブサイトの規則及び業務ニーズに基づいてデータを抽出し、中間倉庫のデータ洗浄/フォーマットに保存し、ターゲットライブラリに保存する。
Crawler 4 j+jsoupに基づいて爬虫類を実現します。
複数スレッドのweb爬虫をCrawler 4 jで構築し、ページの内容をキャプチャする。Crawler 4 jの使用は、2つのステップに分けられる。
(1)WebCrawlerから引き継ぐ爬虫類を実現するには、二つの主要な方法をカバーする必要がある:ShuldVisit:この方法はキャプチャするURLとその内容を決定している。visit:URLのダウンロードが完了すると、この方法が呼び出されます。jsoup解析HTMLを使って、jQuery選択の文法を採用することができます。2)CrawController呼び出しにより実現される爬虫類。キャプチャされたシード(seed)、中間データ格納されたフォルダ、併発スレッドの数などの情報を指定して、コントローラクラスを実現します。
サンプルコード
pom.xmlにおける依存パッケージ設定
(1)爬虫類
1.分布型爬虫類Nutch 2.Java単独機爬虫類Crawler 4 j、WebMagic、WebCollect 3.Java単独機爬虫類Scrrapyではない
開発の考え方
業務ニーズに応じて適切な爬虫枠を選択し、ウェブサイトの規則及び業務ニーズに基づいてデータを抽出し、中間倉庫のデータ洗浄/フォーマットに保存し、ターゲットライブラリに保存する。
Crawler 4 j+jsoupに基づいて爬虫類を実現します。
複数スレッドのweb爬虫をCrawler 4 jで構築し、ページの内容をキャプチャする。Crawler 4 jの使用は、2つのステップに分けられる。
(1)WebCrawlerから引き継ぐ爬虫類を実現するには、二つの主要な方法をカバーする必要がある:ShuldVisit:この方法はキャプチャするURLとその内容を決定している。visit:URLのダウンロードが完了すると、この方法が呼び出されます。jsoup解析HTMLを使って、jQuery選択の文法を採用することができます。2)CrawController呼び出しにより実現される爬虫類。キャプチャされたシード(seed)、中間データ格納されたフォルダ、併発スレッドの数などの情報を指定して、コントローラクラスを実現します。
サンプルコード
pom.xmlにおける依存パッケージ設定
edu.uci.ics
crawler4j
4.2
org.jsoup
jsoup
1.10.1
コード(1)爬虫類
public class MyCrawler extends WebCrawler {
private final static Pattern FILTERS = Pattern.compile(".*(\\.(css|js|gif|jpg"
+ "|png|mp3|mp3|zip|gz))$");
/**
* This method receives two parameters. The first parameter is the page
* in which we have discovered this new url and the second parameter is
* the new url. You should implement this function to specify whether
* the given url should be crawled or not (based on your crawling logic).
* In this example, we are instructing the crawler to ignore urls that
* have css, js, git, ... extensions and to only accept urls that start
* with "http://www.ics.uci.edu/". In this case, we didn't need the
* referringPage parameter to make the decision.
*/
@Override
public boolean shouldVisit(Page referringPage, WebURL url) {
String href = url.getURL().toLowerCase();
return !FILTERS.matcher(href).matches()
&& href.startsWith("http://www.ics.uci.edu/");
}
/**
* This function is called when a page is fetched and ready
* to be processed by your program.
*/
@Override
public void visit(Page page) {
String url = page.getWebURL().getURL();
System.out.println("URL: " + url);
if (page.getParseData() instanceof HtmlParseData) {
HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
String text = htmlParseData.getText();
String html = htmlParseData.getHtml();
Set 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());
}
}
}
(2)Controller呼び出しpublic class Controller {
public static void main(String[] args) throws Exception {
String crawlStorageFolder = "/data/crawl/root";
int numberOfCrawlers = 7;
CrawlConfig config = new CrawlConfig();
config.setCrawlStorageFolder(crawlStorageFolder);
/*
* Instantiate the controller for this crawl.
*/
PageFetcher pageFetcher = new PageFetcher(config);
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
/*
* For each crawl, you need to add some seed urls. These are the first
* URLs that are fetched and then the crawler starts following links
* which are found in these pages
*/
controller.addSeed("http://www.ics.uci.edu/~lopes/");
controller.addSeed("http://www.ics.uci.edu/~welling/");
controller.addSeed("http://www.ics.uci.edu/");
/*
* Start the crawl. This is a blocking operation, meaning that your code
* will reach the line after this only when crawling is finished.
*/
controller.start(MyCrawler.class, numberOfCrawlers);
}
}