iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 7
0
Big Data

研究爬蟲的世界系列 第 7

研究爬蟲的世界 - Simple Crawler 常用事件

crawlstart

當爬蟲開始跟 restarted 的時候會觸發的事件

crawler.on("crawlstart", function() {
    console.log("Crawl starting");
});

fetchstart

fetchstart (queueItem, requestOptions) - Fired when an item is spooled for fetching. If your event handler is synchronous, you can modify the crawler request options (including headers and request method.)

crawler.on("fetchstart", function(queueItem, requestOptions) {
    console.log("fetchStart", queueItem);
});

fetchcomplete

抓取完成的時候會觸發的事件,responseBody 預設是 buffer,所以取值時要用 responseBody.toString()

crawler.on("fetchcomplete", function(queueItem, responseBody, responseObject) {
    console.log("fetchcomplete", queueItem);
    console.log("body", responseBody.toString());
});

fetcherror

當抓取時發生 HTTP error 的時候會觸發的事件

crawler.on("fetcherror", function(queueItem, responseObject) {
    console.log("fetch error!");
});

complete

當爬蟲已經沒有東西可以爬,而且 queue 都做完的時候會觸發的事件。

crawler.on("complete", function() {
    console.log("Finished!");
});


上一篇
研究爬蟲的世界:真實範例 - 爬 MSN 新聞標題
下一篇
研究爬蟲的世界 - Simple Crawler 常用配置
系列文
研究爬蟲的世界15
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言