网络爬虫入门教程(一):概述

爬虫基础:

编写神箭手爬虫, 需要具备以下技能:

爬虫采用JavaScript编写

从网页中抽取数据需要用XPath

(后面会开放支持CSS选择器)

很多情况下都会用到正则表达式

在某些情况下, 你可能还需要用到JsonPath

Chrome的开发者工具

是神器, 很多AJAX请求需要用它来分析


第一个demo​

爬虫采用JavaScript编写, 下面以糗事百科为例, 来看一下我们的爬虫长什么样子:​

    var configs = {

    // configs对象的成员domains, scanUrls, contentUrlRegexes和fields

    domains: ["www.qiushibaike.com"],

    scanUrls: ["http://www.qiushibaike.com/"],

    contentUrlRegexes: ["http://www\\.qiushibaike\\.com/article/\\d+"],

    fields: [

    {

    name: "content", // fields成员中第一个field对象的name

    selector: "//*[@id='single-next-link']", // fields成员中第一个field对象的selector

    required: true // fields成员中第一个field对象的required

    },

    {

    name: "author", // fields成员中第二个field对象的name

    selector: "//div[contains(@class,'author')]//h2" // fields成员中第二个field对象的selector

    }

    ]

    };

    // 向爬虫任务中添加configs配置,并启动爬虫

    var crawler = new Crawler(configs);

    crawler.start();


爬虫的整体框架就是这样, 首先定义了一个configs对象, 里面设置了待爬网站的一些信息, 然后通过调用var crawler = new Crawler(configs);和crawler.start();来配置并启动爬虫.

configs对象如何定义, 后面会作详细介绍。

参考资料:http://doc.shenjianshou.cn/

神箭手云爬虫官网地址:http://www.shenjianshou.cn/​

爬虫demo源码分享:https://github.com/ShenJianShou/crawler_samples

猜你喜欢

转载自2879835984.iteye.com/blog/2306914