Giter Club home page Giter Club logo

spider's Introduction

Java简易爬虫框架

核心是实现ISpider接口,实现对应的方法即可进行简单抓取操作

解析页面内容

    
    /**
     * 解析内容
     *
     * @param response
     */
    void parse(Response response);
    

例:

    @Override
    public void parse(Response response) {
        //创建抓取模板
        Template template = Template.me().type(Type.HTML).expression("a.v-align-middle");
        response.template(template);
        //根据模板配置,提取对应内容
        Iterator<Element> elements = response.domParse().iterator();
        while (elements.hasNext()) {
            LOG.info("parse text O(∩_∩)O  => {}", elements.next().text());
        }
    }

发现更多内容

    
    /**
     * 发现更多内容
     *
     * @return
     */
    List<String> discoverMore(Response response);

例:

    @Override
    public List<String> discoverMore(Response response) {
        //配置模板
        Template template = Template.me().type(Type.HTML).expression("a.next_page");
        response.template(template);
        //根据模板解析
        Element element = response.domParse().first();
        String url = element.attr("href");
        url = "https://github.com" + url;
        String finalUrl = url;
        if (Regex.match(finalUrl, "^https://github\\.com/search\\?p=([\\d]+)\\&").equals("2")) {
            Scheduler.shutdown();
        }
        //将提取出的链接放回调度当中
        return new ArrayList<String>() {{
            add(finalUrl);
        }};
    }

启动方式

    
    public static void main(String[] args) {
            new Scheduler("https://github.com/search?utf8=%E2%9C%93&q=java", new Github()).running();
    }

spider's People

Contributors

gloriabing avatar

Watchers

James Cloos avatar  avatar

Forkers

xiaonaonao

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.