A study of Java crawler

Source: Internet
Author: User

Cause

Recently suddenly sent out of epilepsy crazy, is very interested in reptiles, began to write a few crawler practice practiced hand, so, wash hands open.

Lazy people like me, know a reptile about the crawler began to lazy, began to look for the framework, Google keyword "Java crawler", the first search results are

High votes to answer the recommended several crawler framework: Nutch, Heritrix, crawler4j, Webcollector and WebMagic, decisive choice webmagic, support the work of the people ( must be Chinese documents AH )

Start

Using MAVEN to add frames to your project, add the following dependencies in the Poxm.xml file. the domestic Maven library incredibly no webmagic, tears rush, the delay added to the dependency.

<dependency>        <groupId>us.codecraft</groupId>        <artifactid>webmagic-core</ artifactid>        <version>0.5.3</version></dependency><dependency>        <groupid >us.codecraft</groupId>        <artifactId>webmagic-extension</artifactId>        <version >0.5.3</version></dependency>

Dependencies are added successfully as shown in:

Starting with an example of a development document, there are three main sections:

    • Crawl site-related configuration
    • How to extract page information
    • Discover subsequent URL addresses

WebMagic provides a site class for setting the associated properties of a crawl site, for example, setting a retry count of 3 times for failed settings, and a crawl interval of 1 seconds, as shown in the following code:

Private Site site = site.me (). Setretrytimes (3). Setsleeptime (1000);
How to extract page information

WebMagic implements the basic logic function of the crawler by rewriting the Pageprocessor process () method.

WebMagic provides four ways to extract data:

For HTML content, you can extract content through the three methods of XPath, CSS selector, and regular expression, and for JSON-formatted data, you can extract the content through the Jsonpath method.

Here is the API table for extracting the content:

Method Description Example
XPath (String XPath) Using XPath to select Html.xpath ("//div[@class = ' title ']")
$ (String selector) Use the CSS selector to select html.$ ("Div.title")
$ (String selector,string attr) Use the CSS selector to select html.$ ("Div.title", "text")
CSS (String selector) function with $ (), select with CSS selector Html.css ("Div.title")
Links () Select All Links Html.links ()
Regex (String regex) Extracting using regular expressions Html.regex ("\<div\> (. *?) \")
Regex (String regex,int Group) Use regular expression extraction and specify the capturing group Html.regex ("\<div\> (. *?) \ ", 1)
Replace (string regex, string replacement) Replace content Html.replace ("\", "" ")

Discover subsequent URL addresses

The page of a site is very much, then how do we find and extract a page after the chain of mining it? This is a very critical part of the crawler, the chain-

The answer is to construct the URL format to be mined through regular expressions, to invoke the Addtargetrequests () method of the Page object (which is not the pages object, which is the parameter object provided by the process () method), and add the URL to the URL to be mined.

A study of Java crawler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.