爬虫技术(02)神箭手爬虫实时API

var scanUrl = "http://www.leiphone.com/news/201610/9EbxjV73a9kGhN3a.html"
;//@input(scanUrl, 文章url, 请输入正确的url)

var configs = {
    domains: ["leiphone.com"],
    scanUrls: [scanUrl],
    fields: [
        {
            // 抽取文章页面的标题
            name: "article_title",
            selector: "//h1[contains(@class,'headTit')]",
            required: true
        },
        {
            // 抽取文章页面的内容
            name: "article_content",
            selector: "//div[contains(@class,'lph-article-comView')]",
            required: true
        },
        {
            // 抽取文章页面的发布日期
            name: "article_publish_time",
            selector: "//td[contains(@class,'time')]",
            required: true
        },
        {
            // 抽取文章页面的作者
            name: "article_author",
            selector: "//td[contains(@class,'aut')]/a",
            required: true
        }
    ]
};

// 在"afterExtractField回调函数"中将爬取到的时间转换为秒级时间戳
configs.afterExtractField = function(fieldName, data, page, site) {
    if (fieldName == "article_publish_time") {
        var timestamp = Date.parse(data);
        return isNaN(timestamp) ? 0 : parseInt(timestamp/1000);
    }
    return data;
};

// 使用以上配置创建一个实时API
var fetcher = new Fetcher(configs);
// 运行实时API
fetcher.start();

你可能感兴趣的:(前端,爬虫)