For you to recommend a very good Java writing vertical crawler webmagic, the force is very

Source: Internet
Author: User

WebMagic Official Introduction: http://blog.csdn.net/u010670689/article/details/41704239
Goal of this article: crawl the site's head content, then console print WebMagic version: 0.5.1maven coordinates:
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>0.5.1</version>
</dependency>



1. To achieve their own pageprocessorpageprocessor is the core of the crawler, you can define the crawler crawl rules, I will pageprocessor the customization is divided into three parts, respectively, crawler configuration, page element extraction and link discovery. Code:

Package Youling.studio;


Import Us.codecraft.webmagic.Page;
Import Us.codecraft.webmagic.Site;
Import Us.codecraft.webmagic.Spider;
Import Us.codecraft.webmagic.pipeline.ConsolePipeline;
Import Us.codecraft.webmagic.pipeline.JsonFilePipeline;
Import Us.codecraft.webmagic.processor.PageProcessor;
Import Us.codecraft.webmagic.processor.example.GithubRepoPageProcessor;


public class Mypageprocessor implements Pageprocessor {


Part One: Crawl the relevant configuration of the site, including encoding, crawl interval, number of retries, etc.
Private Site site = site.me (). Setretrytimes (3). Setsleeptime (1000);


Process is the core interface of custom crawler logic, where the extraction logic is written
@Override
public void Process (Page page) {
Section Two: Define how to extract page information and save it
//
<span class= "Link_title" ><a href= "/u010670689/article/details/41576647" >
Start Hive Metastore Command
</a></span>

HTML code for the CSDN article title
Page.putfield ("title", Page.gethtml (). XPath ("//h1/span[@class = ' link_title ']/a/text ()"). ToString ());
if (Page.getresultitems (). Get ("title") = = null) {
Skip this page
Page.setskip (TRUE);
}


Part Three: Crawling from the page to find subsequent URL addresses
// <span class= "Link_title" ><a href= "/u010670689/article/details/41704239" >
WebMagic General Introduction
</a></span>
Page.addtargetrequests (Page.gethtml (). Links (). Regex ("(/u010670689/article/details/(\\w+))"). All ());
}


@Override
Public Site Getsite () {
return site;
}


public static void Main (string[] args) {
Spider.create (New Mypageprocessor ())
Start scratching from "Https://github.com/code4craft"
. Addurl ("Http://blog.csdn.net/u010670689?viewmode=contents")
Save to JSON file
. Addpipeline (New Consolepipeline ())
Open 5 Thread Crawl
. Thread (1)
Start crawler
. Run ();
}
}




Configuration of the 4.1.1 Crawler

The first part of the crawler configuration, including encoding, crawl interval, time-out, retries, and so on, including some simulation parameters, such as user agent, cookie, and proxy settings, we will be in the 5th chapter-"Crawler Configuration" in the introduction. Here we have a simple set up: The number of retries is 3 times, crawl interval of one second.

4.1.2 Extraction of page elements

The second part is the core part of the crawler: How do you extract the information you want from the HTML page you downloaded to? The webmagic uses three extraction techniques: XPath, regular expression, and CSS selector. In addition, for JSON-formatted content, you can use Jsonpath for parsing.

    1. Xpath

      XPath is a query language used to get elements in XML, but it is also convenient for HTML. For example:

       page.getHtml().xpath("//h1[@class=‘entry-title public‘]/strong/a/text()")

      This code uses XPath, which means "find all H1 elements with the class attribute as ' entry-title public ' and find a child node of his strong child node, and extract the text information of the A node". The corresponding HTML is this:

    2. CSS Selector

      CSS selectors are languages that are similar to XPath. If you have done front-end development, you certainly know the meaning of the $ (' h1.entry-title ') notation. Objectively speaking, it is easier to write than XPath, but if you write a more complicated extraction rule, it's a bit more cumbersome.

    3. Regular expressions

      Regular expressions are a common text extraction language.

       page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());

      This code uses a regular expression that represents a link that matches all "https://github.com/code4craft/webmagic".

    4. JsonPath

      Jsonpath is a language that is very similar to XPath, which is used to quickly locate a piece of content from JSON. The Jsonpath format used in WebMagic can be consulted here: https://code.google.com/p/json-path/

Discovery of 4.1.3 Links

With the logic of handling the page, our crawler is nearing completion!

But now there is a problem: a Site page is a lot of, at first we can not all listed, so how to find the follow-up link, is an indispensable part of a crawler.

page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());

This code is divided into two parts to page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all() obtain all the "(https://github\.com/\w+/\w+)" The regular expression of the link, then add page.addTargetRequests() these links to the queue to be crawled.





Where the code in the pipeline can be customized, Scheduler,downloader can be customized, the following introduction

For you to recommend a very good Java writing vertical crawler webmagic, the force is very

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.