webmagic爬虫

<dependency>
            <groupId>us.codecraft</groupId>
            <artifactId>webmagic-core</artifactId>
            <version>0.7.3</version>
        </dependency>
        <dependency>
            <groupId>us.codecraft</groupId>
            <artifactId>webmagic-extension</artifactId>
            <version>0.7.3</version>
        </dependency>

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

public class GithubRepoPageProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

    public Site getSite() {
        return site;
    }

    public void process(Page page) {
        System.out.println(page.getUrl());
        System.out.println(page.getHtml());
        Html html = page.getHtml();
        List<String> strs = html.xpath("//h2/a/text()").all();
        List<String> srcs = page.getHtml().xpath("//img/@src").all();
        System.out.println(strs);
        System.out.println(srcs);
    }

    public static void main(String[] args) {
        Spider.create(new GithubRepoPageProcessor()).addUrl("https://blog.csdn.net/u013491262").thread(5).run();
    }

}
相关文章
相关标签/搜索