W3C api 抓取

存档

W3CIDLFetcher

package canghailan.w3c;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.BufferedWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @author canghailan 2011-12-26 14:54
 */
public class W3CIDLFetcher {

    private static final int TIMEOUT = 60 * 1000; // 1min
    private static final String[] CSS_URLS = {
            "http://dev.w3.org/csswg/cssom/",
            "http://dev.w3.org/csswg/cssom-view/"
    };
    private static final String[] DOM_URLS = {
            "http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html",
            "http://www.w3.org/TR/DOM-Level-3-Events/"
    };
    private static final String[] HTML_URLS = {
            "http://www.w3.org/TR/html5/Overview.html",
    };

    public static String[] fetchAll() throws IOException, URISyntaxException {
        Path home = Paths.get(W3CIDLFetcher.class.getProtectionDomain()
                .getCodeSource().getLocation().toURI());
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        Path cssIdl = home.resolve("css" + timestamp + ".idl");
        Path domIdl = home.resolve("dom" + timestamp + ".idl");
        Path htmlIdl = home.resolve("html" + timestamp + ".idl");

        fetch(Arrays.asList(CSS_URLS), cssIdl.toString());
        fetch(Arrays.asList(DOM_URLS), domIdl.toString());
        fetch(Arrays.asList(HTML_URLS), htmlIdl.toString());

        return new String[]{cssIdl.toString(), domIdl.toString(), htmlIdl.toString()};
    }

    public static void fetch(Iterable<String> urls, String path) throws IOException {
        for (String url : urls) {
            fetch(url, path);
        }
    }

    public static void fetch(String url, String path) throws IOException {
        System.out.println("fetch idl: " + url + " -> " + path);

        Document doc = Jsoup.connect(url).timeout(TIMEOUT).get();
        BufferedWriter writer = Files.newBufferedWriter(
                Paths.get(path), Charset.forName("UTF-8"),
                StandardOpenOption.WRITE,
                StandardOpenOption.CREATE,
                StandardOpenOption.APPEND);
        for (Element idl : doc.select("pre.idl")) {
            writer.write(idl.text());
            writer.write("\n\n");
        }
        writer.flush();
        writer.close();
    }

    public static void main(String[] args) throws IOException, URISyntaxException {
        fetchAll();
    }

}

WebIDLGrammarDownloader

package canghailan.webidl;

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

import java.io.BufferedWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author canghailan 2011-12-26 19:39
 */
public class WebIDLGrammarDownloader {

    private static final int TIMEOUT = 60 * 1000; // 1min

    public static void download() throws IOException, URISyntaxException {
        Document doc = Jsoup.connect("http://www.w3.org/TR/WebIDL/").timeout(TIMEOUT).get();
        Elements grammars = doc.select("#appendices .grammar");

        Path home = Paths.get(WebIDLGrammarDownloader.class.getProtectionDomain()
                .getCodeSource().getLocation().toURI());
        Path webIdlGrammar = home.resolve("web-idl.g");
        BufferedWriter writer = Files.newBufferedWriter(
                webIdlGrammar, Charset.forName("UTF-8"),
                StandardOpenOption.WRITE,
                StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING);

        for (Element grammar : grammars) {
            for (Element tr : grammar.select("tr")) {
                writer.write(tr.text());
                writer.write('\n');
            }
        }

        writer.flush();
        writer.close();
    }

    public static void main(String[] args) throws IOException, URISyntaxException {
        download();
    }
}

w3c-api: https://code.google.com/p/es-operating-system/

转载于:https://my.oschina.net/canghailan/blog/38431

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值