爬虫技术(01)神箭手爬虫初学案例解读
生活随笔
收集整理的這篇文章主要介紹了
爬虫技术(01)神箭手爬虫初学案例解读
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
var configs = {//要爬取的域名domains: ["leiphone.com"], //爬蟲的入口URL scanUrls: ["http://www.leiphone.com/search?s=vr&site=article"],//內(nèi)容頁url正則/*解讀下面正則:http://www.leiphone.com/news/ (\d+表示任意位數(shù)的數(shù)字) /(.+表示任意字符多次或1次).html\d 匹配一個(gè)字?jǐn)?shù)字符,/\d/ = /[0-9]/+ 匹配前面元字符1次或多次 . 可以替代任何字符 例如:https://www.leiphone.com/news/201701/ri7dDB7Fq0mMj1qh.html*/contentUrlRegexes: [/http:\/\/www\.leiphone\.com\/news\/\d+\/.+\.html/],//列表頁url正則 可留空/*解讀下面正則:http://www.leiphone.com/search?s=vr&site=article(&page=\d+)? 表示頁數(shù)可有不可有例如:https://www.leiphone.com/search?s=vr&site=article&page=2*/helperUrlRegexes: [/http:\/\/www\.leiphone\.com\/search\?s=vr&site=article(&page=\d+)?/],fields: [{// 抽取內(nèi)容頁的文章標(biāo)題 // name自己寫name: "article_title",// selector解讀 表示該name的值是頁面上的h1標(biāo)簽上的class="headTit"的頁簽的值/*<h1 class="headTit">4K之后,8K頭顯海外眾籌表現(xiàn)不俗,小派科技為什么要死磕分辨率? </h1>*/selector: "//h1[contains(@class,'headTit')]",required: true},{// 抽取內(nèi)容頁的文章內(nèi)容/**/name: "article_content",selector: "//div[contains(@class,'lph-article-comView')]",required: true},{// 抽取內(nèi)容頁的文章發(fā)布日期name: "article_publish_time",selector: "//td[contains(@class,'time')]",required: true},{// 抽取內(nèi)容頁的文章作者/*如果出現(xiàn)這種情況 就是定位的里面還有標(biāo)簽 那么就如下寫<td class="aut">本文作者:<a href="https://www.leiphone.com/author/tianmiao" target="_blank" rel="nofollow">田苗</a></td>*/name: "article_author",selector: "//td[contains(@class,'aut')]/a",required: true}]
};// 在"afterExtractField回調(diào)函數(shù)"中將爬取到的時(shí)間轉(zhuǎn)換為秒級(jí)時(shí)間戳
/*fieldName: 表示字段名稱data: 表示字段值page: 表示當(dāng)前正在爬取的網(wǎng)頁對象site: 表示當(dāng)前正在爬取的目標(biāo)網(wǎng)站對象
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;
};// 使用以上配置創(chuàng)建一個(gè)采集爬蟲
var crawler = new Crawler(configs);
// 啟動(dòng)該采集爬蟲
crawler.start();
總結(jié)
以上是生活随笔為你收集整理的爬虫技术(01)神箭手爬虫初学案例解读的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 该网页无法正常运作 HTTP ERROR
- 下一篇: 云计算HCIA学习笔记-云计算基础概念