|
@@ -0,0 +1,476 @@
|
|
|
+
|
|
|
+<!DOCTYPE HTML>
|
|
|
+<html lang="" >
|
|
|
+ <head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
|
+ <title>新建规则 · GitBook</title>
|
|
|
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
|
+ <meta name="description" content="">
|
|
|
+ <meta name="generator" content="GitBook 3.2.3">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <link rel="stylesheet" href="../gitbook/style.css">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <meta name="HandheldFriendly" content="true"/>
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
|
|
+ <meta name="apple-mobile-web-app-capable" content="yes">
|
|
|
+ <meta name="apple-mobile-web-app-status-bar-style" content="black">
|
|
|
+ <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
|
|
|
+ <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+
|
|
|
+<div class="book">
|
|
|
+ <div class="book-summary">
|
|
|
+
|
|
|
+
|
|
|
+<div id="book-search-input" role="search">
|
|
|
+ <input type="text" placeholder="Type to search" />
|
|
|
+</div>
|
|
|
+
|
|
|
+
|
|
|
+ <nav role="navigation">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+<ul class="summary">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.1" data-path="../">
|
|
|
+
|
|
|
+ <a href="../">
|
|
|
+
|
|
|
+
|
|
|
+ 简介
|
|
|
+
|
|
|
+ </a>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.2" >
|
|
|
+
|
|
|
+ <span>
|
|
|
+
|
|
|
+
|
|
|
+ 安装以及配置
|
|
|
+
|
|
|
+ </span>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <ul class="articles">
|
|
|
+
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.2.1" data-path="../install/">
|
|
|
+
|
|
|
+ <a href="../install/#install">
|
|
|
+
|
|
|
+
|
|
|
+ 安装
|
|
|
+
|
|
|
+ </a>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.2.2" data-path="../install/">
|
|
|
+
|
|
|
+ <a href="../install/#config">
|
|
|
+
|
|
|
+
|
|
|
+ 配置
|
|
|
+
|
|
|
+ </a>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+
|
|
|
+ </ul>
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.3" >
|
|
|
+
|
|
|
+ <span>
|
|
|
+
|
|
|
+
|
|
|
+ 目录结构
|
|
|
+
|
|
|
+ </span>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <ul class="articles">
|
|
|
+
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.3.1" data-path="../code/">
|
|
|
+
|
|
|
+ <a href="../code/#code">
|
|
|
+
|
|
|
+
|
|
|
+ 目录
|
|
|
+
|
|
|
+ </a>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+
|
|
|
+ </ul>
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.4" >
|
|
|
+
|
|
|
+ <span>
|
|
|
+
|
|
|
+
|
|
|
+ 部署
|
|
|
+
|
|
|
+ </span>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <ul class="articles">
|
|
|
+
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.4.1" data-path="../run/">
|
|
|
+
|
|
|
+ <a href="../run/#run">
|
|
|
+
|
|
|
+
|
|
|
+ 部署
|
|
|
+
|
|
|
+ </a>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+
|
|
|
+ </ul>
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.5" >
|
|
|
+
|
|
|
+ <span>
|
|
|
+
|
|
|
+
|
|
|
+ 使用
|
|
|
+
|
|
|
+ </span>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <ul class="articles">
|
|
|
+
|
|
|
+
|
|
|
+ <li class="chapter " data-level="1.5.1" data-path="./">
|
|
|
+
|
|
|
+ <a href="./#规则">
|
|
|
+
|
|
|
+
|
|
|
+ 新建规则
|
|
|
+
|
|
|
+ </a>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+
|
|
|
+ </ul>
|
|
|
+
|
|
|
+ </li>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <li class="divider"></li>
|
|
|
+
|
|
|
+ <li>
|
|
|
+ <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
|
|
|
+ Published with GitBook
|
|
|
+ </a>
|
|
|
+ </li>
|
|
|
+</ul>
|
|
|
+
|
|
|
+
|
|
|
+ </nav>
|
|
|
+
|
|
|
+
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div class="book-body">
|
|
|
+
|
|
|
+ <div class="body-inner">
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+<div class="book-header" role="navigation">
|
|
|
+
|
|
|
+
|
|
|
+ <!-- Title -->
|
|
|
+ <h1>
|
|
|
+ <i class="fa fa-circle-o-notch fa-spin"></i>
|
|
|
+ <a href=".." >新建规则</a>
|
|
|
+ </h1>
|
|
|
+</div>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <div class="page-wrapper" tabindex="-1" role="main">
|
|
|
+ <div class="page-inner">
|
|
|
+
|
|
|
+<div id="book-search-results">
|
|
|
+ <div class="search-noresults">
|
|
|
+
|
|
|
+ <section class="normal markdown-section">
|
|
|
+
|
|
|
+ <h1 id="规则">规则</h1>
|
|
|
+<blockquote>
|
|
|
+<p>新建规则,规则对应需要抓取的相同类型的网页,比如列表页,详情页
|
|
|
+无论是 /list?page=1 还是 /list?page=n 只要是抓取这个类型的页面获取的数据结构一样的都可以归类成一个规则内</p>
|
|
|
+</blockquote>
|
|
|
+<p><img src="../img/图3-1.jpg" alt="图片"></p>
|
|
|
+<ul>
|
|
|
+<li><p>规则id是唯一的</p>
|
|
|
+</li>
|
|
|
+<li><p>示例url是此规则的举例url,在此url上写采集规则</p>
|
|
|
+</li>
|
|
|
+<li><p>请求模式:普通和浏览器模式,默认普通模式,如果页面是js渲染的则用浏览器模式</p>
|
|
|
+</li>
|
|
|
+<li><p>数据类型:html/json,如果是爬取的api那么使用json,如果是普通页面则用html</p>
|
|
|
+</li>
|
|
|
+<li><p>等待条件,如果是请求模式选择浏览器模式,那么爬虫会根据这里设置的等待条件等待页面加载出这个等待的元素后才回调返回此页面的渲染好的html</p>
|
|
|
+</li>
|
|
|
+<li><p>页面预处理,可以在爬虫获取返回html后先处理页面html,然后再接下去用选择器获取去页面数据,比如有些页面是404,此404是一张图片,如果是直接传下去给选择器,那么会报错缺少字段,如果是预处理时就判断是否404图片,那么直接返回当做抓取成功不会进入选择器。$html, $, page,_task, JTool, Tool 这几个变量可以直接使用,在爬虫内部定义如下:</p>
|
|
|
+</li>
|
|
|
+</ul>
|
|
|
+<pre><code> async _preprocess(content, page) {
|
|
|
+ if (this.rule.data_type === 'json') {
|
|
|
+ content = content.trim();
|
|
|
+ let lastChar = php.substr(content, -1);
|
|
|
+ if (lastChar === ')') {
|
|
|
+ let pos = content.indexOf('(');
|
|
|
+ content = content.substr(pos + 1, content.length - pos - 2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let preprocess = this.rule.preprocess && this.rule.preprocess.trim();
|
|
|
+ if (preprocess) {
|
|
|
+ let func = php.create_function('$html, $, page, _task, JTool, Tool', preprocess);
|
|
|
+
|
|
|
+ let $ = null;
|
|
|
+ let $html = null;
|
|
|
+ if (this.rule.data_type === 'html') {
|
|
|
+ $ = cheerio.load(content, { decodeEntities: false });
|
|
|
+ JTool.initJquery($);
|
|
|
+
|
|
|
+ $html = $('html');
|
|
|
+ let flag = func($html, $, page, this.task, JTool, Tool);
|
|
|
+ if (flag === false) {
|
|
|
+ this.skip = true;
|
|
|
+ }
|
|
|
+ return $('<div></div>').html($html).html();
|
|
|
+ } else if (this.rule.data_type === 'json') {
|
|
|
+ $html = content;
|
|
|
+ return func($html, $, page, JTool, Tool);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+</code></pre><hr>
|
|
|
+<h1 id="选择器">选择器</h1>
|
|
|
+<blockquote>
|
|
|
+<p>选择器负责拾取页面的数据,在浏览器上用js调试,可以立马生效查看获取的数据,这是可视化的基础,而不必如同其他爬虫一样需要每个网页手写不同的选择器</p>
|
|
|
+</blockquote>
|
|
|
+<p><img src="../img/图3-2.jpg" alt="图片"></p>
|
|
|
+<ul>
|
|
|
+<li>选择器:查找页面元素并返回</li>
|
|
|
+<li>解析$el:上面选择器获取的节点元素,返回的变量用 $el 表示,和jquery操作节点元素一致<pre><code>可用变量如下
|
|
|
+_task:任务对象,[ 'url', 'rule_id', 'task_id', 'task_key' ]
|
|
|
+JTool.fixColspan($table, $): 展开表格colspan,删除不合法的列
|
|
|
+JTool.formatDate(date): 把int/Date类型的数据,格式化为:Y-m-d
|
|
|
+JTool.formatDateTime(date): 把int/Date类型的数据,格式化为:Y-m-d H:i:s
|
|
|
+JTool.formatUrl(url): 把相对地址变化成绝对地址
|
|
|
+JTool.formaRichText(content): 富文本的图片相对地址变化成绝对地址,去掉script标签
|
|
|
+JTool.md5(str): md5加密
|
|
|
+</code></pre></li>
|
|
|
+<li>Next规则id:指定当爬取玩此规则后进入的下一个规则,可以设置上下文逻辑,比如列表页面的下一个规则应该是详情页</li>
|
|
|
+<li>选择 单项/多项:如果是爬取的数据是多行的则选择多项,如果是只有一项那么选择单项。这里插入的逻辑是,如果是多项的取各个列名字段相同下标的数据处理组成一条数据记录插入数据库例如:
|
|
|
+```
|
|
|
+a 列名:选择多项获取到的是一个数组 ['a', 'b', 'c']
|
|
|
+b 列名:选择多项获取到的是一个数组 ['d', 'e', 'f']</li>
|
|
|
+</ul>
|
|
|
+<p>爬虫会将上面的数据格式化成:
|
|
|
+[['a','d'], ['b','e'], ['c','f']]
|
|
|
+逐条插入/更新到数据库
|
|
|
+```</p>
|
|
|
+<ul>
|
|
|
+<li>数据-仅插入/更新/仅更新:仅插入(insert)是当数据是新数据的时候插入到数据库,第二次爬取的时候数据不会再更新入库,更新(replace)是当第二次爬取的时候会执行更新到数据库,仅更新(update)是第二次爬取的数据和当前的数据库的数据不一致才会入库更新</li>
|
|
|
+<li>必填:如果是在选择器分析页面数据中没有此字段,那么会报错,如果是可选,则不会报错</li>
|
|
|
+<li>开关:开启后选择器会解析页面抓取数据,否则不进入此选择器的逻辑</li>
|
|
|
+<li>转存:如果是图片,可以将此图片转存至自己服务器然后更新到数据库,这是一个异步的过程,入库后,转存脚本会扫描需要转存的数据,如果没有转存则将其下载到bs2然后重新将新的资源url地址更新到数据库内</li>
|
|
|
+<li>只填充:只填充的字段,只在更新模式有效,选择器获取的数据会和临时表中上次抓取的老数据对比,老数据不存在时才填充</li>
|
|
|
+</ul>
|
|
|
+<hr>
|
|
|
+<h1 id="配置库表">配置库表</h1>
|
|
|
+<blockquote>
|
|
|
+<p>爬虫的数据库表从名字服务器内的配置拉取到表 db_table,data_db中</p>
|
|
|
+</blockquote>
|
|
|
+<p><img src="../img/图4-3.jpg" alt="图片">
|
|
|
+<img src="../img/图4-2.jpg" alt="图片"></p>
|
|
|
+<hr>
|
|
|
+<h1 id="任务">任务</h1>
|
|
|
+<blockquote>
|
|
|
+<p>当规则添加后,需要配置任务,爬虫才能进行入库 </p>
|
|
|
+</blockquote>
|
|
|
+<p><img src="../img/图4-1.jpg" alt="图片"></p>
|
|
|
+<ul>
|
|
|
+<li>爬虫的地址:指的爬虫的初始入口url,爬虫任务根据此url,开始爬取,如果你配置了Next规则id,那么爬虫当爬取完此规则后会自动进入下一规则,这样就实现了上下问逻辑的网页爬取</li>
|
|
|
+</ul>
|
|
|
+<p>当添加完任务后还可以在网页上尝试点击执行,网页返回的是此任务执行期间的打印的日志
|
|
|
+<img src="../img/图4-4.jpg" alt="图片">
|
|
|
+<img src="../img/图4-7.jpg" alt="图片"></p>
|
|
|
+<hr>
|
|
|
+<h1 id="调试">调试</h1>
|
|
|
+<blockquote>
|
|
|
+<p>通过日志和网页运行爬取任务可以调试一个规则是否有报错</p>
|
|
|
+</blockquote>
|
|
|
+<p><img src="../img/图4-6.jpg" alt="图片">
|
|
|
+<img src="../img/图4-5.jpg" alt="图片"></p>
|
|
|
+
|
|
|
+
|
|
|
+ </section>
|
|
|
+
|
|
|
+ </div>
|
|
|
+ <div class="search-results">
|
|
|
+ <div class="has-results">
|
|
|
+
|
|
|
+ <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
|
|
|
+ <ul class="search-results-list"></ul>
|
|
|
+
|
|
|
+ </div>
|
|
|
+ <div class="no-results">
|
|
|
+
|
|
|
+ <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
|
|
|
+
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+</div>
|
|
|
+
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ </div>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <script>
|
|
|
+ var gitbook = gitbook || [];
|
|
|
+ gitbook.push(function() {
|
|
|
+ gitbook.page.hasChanged({"page":{"title":"新建规则","level":"1.5.1","depth":2,"previous":{"title":"使用","level":"1.5","depth":1,"ref":"","articles":[{"title":"新建规则","level":"1.5.1","depth":2,"anchor":"#规则","path":"use/README.md","ref":"use/README.md#规则","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"use/README.md","mtime":"2018-10-10T09:32:56.540Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-10T09:33:12.148Z"},"basePath":"..","book":{"language":""}});
|
|
|
+ });
|
|
|
+ </script>
|
|
|
+</div>
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook.js"></script>
|
|
|
+ <script src="../gitbook/theme.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook-plugin-search/search.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </body>
|
|
|
+</html>
|
|
|
+
|