国庆第三天2014年10月3日10:21:39,Nutz,WebCollector,jsoup

(1)作得好,作得快,只能选择同样。html

(2)时间过得很快,你无法在假期的一天里完成更多的计划。假期所有由本身支配,相对长一点的睡眠,新加入的娱乐(视频或者游戏),你不比在工做中更有效率。java

(3)天天练习一点,记录下来。假期来整合优化巩固,是最好的选择。进步每一天。node

(4)不要太期待假期。git

(5)参照Nutz 入门教程第一讲,作一个小应用。 视频不清晰仍是看完两遍,仍是照着一点点的写出来。视频比较直观,Nutz的文档很详细的,可是任然以为看视频仍是更快,看文档太慢了,或许我看文档的方式要改进下。github

(6)JAVA爬虫 WebCollector: web

官网APIosc简介Cookbook&Apiide

一个读取API的例子:MyParser.java, DocCrawler.java优化

package demo.hello;

import java.io.UnsupportedEncodingException;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.edu.hfut.dmic.webcollector.model.Link;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.parser.HtmlParser;
import cn.edu.hfut.dmic.webcollector.parser.ParseResult;

public class MyParser extends HtmlParser{
    public MyParser(Integer topN) {
        super(topN);
    }

    @Override
    public ParseResult getParse(Page page) throws UnsupportedEncodingException {
        ParseResult parseResult= super.getParse(page);
        Elements frames=page.getDoc().select("frame[src]");
        for(Element frame:frames){
            Link link=new Link();
            link.setAnchor("");
            link.setUrl(frame.attr("abs:src"));
            parseResult.getParsedata().getLinks().add(link);
        }
        return parseResult;
    }
}
View Code
package demo.hello;

import cn.edu.hfut.dmic.webcollector.crawler.BreadthCrawler;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.parser.Parser;
import cn.edu.hfut.dmic.webcollector.util.Config;

public class DocCrawler extends BreadthCrawler{
    @Override
    public Parser createParser(String url, String contentType) throws Exception {
        if(contentType==null)
            return null;
        if(!contentType.contains("text/html"))
            return null;
        return new MyParser(Config.topN);
    }

    public static void main(String[] args) throws Exception{
        DocCrawler crawler=new DocCrawler();
        crawler.addSeed("http://crawlscript.github.io/WebCollectorDoc/");
        crawler.addRegex("http://crawlscript.github.io/WebCollectorDoc.*");
        crawler.setRoot("pages");
        crawler.setThreads(20);
        crawler.start(10);
    }
}
View Code

(7)HTML解析器 jsoup
官网osc简介url

相关文章
相关标签/搜索