


Basic ideas for writing crawlers with Node.js and sharing of examples of crawling Baidu images_node.js
其实写爬虫的思路十分简单:
- 按照一定的规律发送 HTTP 请求获得页面 HTML 源码(必要时需要加上一定的 HTTP 头信息,比如 cookie 或 referer 之类)
- 利用正则匹配或第三方模块解析 HTML 代码,提取有效数据
- 将数据持久化到数据库中
但是真正写起这个爬虫来,我还是遇到了很多的问题(和自己的基础不扎实也有很大的关系,node.js 并没有怎么认真的学过)。主要还是 node.js 的异步和回调知识没有完全掌握,导致在写代码的过程中走了很多弯路。
模块化
模块化对于 node.js 程序是至关重要的,不能像原来写 PHP 那样所有的代码都扔到一个文件里(当然这只是我个人的恶习),所以一开始就要分析这个爬虫需要实现的功能,并大致的划分了三个模块。
主程序,调用爬虫模块和持久化模块实现完整的爬虫功能
爬虫模块,根据传来的数据发送请求,解析 HTML 并提取有用数据,返回一个对象
持久化模块,接受一个对象,将其中的内容储存到数据库中
模块化也带来了困扰了我一个下午的问题:模块之间的异步调用导致数据错误。其实我至今都不太明白问题到底出在哪儿,鉴于脚本语言不那么方便的调试功能,暂时还没有深入研究。
另外一点需要注意的是,模块化时尽量慎用全局对象来储存数据,因为可能你这个模块的一个功能还没有结束,这个全局变量已经被修改了。
Control Flow
这个东西很难翻译,直译叫控制流(吗)。众所周知,node.js 的核心思想就是异步,但是异步多了就会产生好几层嵌套,代码实在难看。这个时候,你需要借助一些 Control Flow 模块来重新整理你的逻辑。在这里就要推荐开发社区十分活跃,用起来也很顺手的 async.js(https://github.com/caolan/async/)。
async 提供了很多实用的方法,我在写爬虫时主要用到了
- async.eachSeries(arr, fn, callback) 依次把 arr 中的每一个元素传给 fn,若 fn 回调没有返回错误对象就继续传下一个,否则把错误对象传给 callback,循环结束
- async.parallel(fn[, fn] , callback) 当所有的 fn 都执行完成后执行 callback
这些控制流方法给爬虫的开发工作带来了很大的方便。考虑这么一个应用场景,你需要把若干条数据插入数据库(属于同一个学生),你需要在所有数据都插入完成后才能返回结果,那么如何保证所有的插入操作都结束了呢?只能是层层回调保证,如果用 async.parallel 就方便多了。
这里再多提一句,本来保证所有的插入都完成这个操作可以在 SQL 层实现,即 transaction,但是 node-mysql 截止我使用的时候还是没有很好的支持 transaction,所以只有自己手动用代码保证了。
解析 HTML
在解析过程中也遇到一些问题,这里一并记录下来。
最基本的发送 HTTP 请求获得 HTML 代码,使用 node 自带的 http.request 功能即可。如果是爬简单的内容,比如获得某个指定 id 元素中的内容(常见于抓去商品价格),那么正则足以完成任务。但是对于复杂的页面,尤其是数据项较多的页面,使用 DOM 会更加方便高效。
而 node.js 最好的 DOM 实现非 cheerio(https://github.com/MatthewMueller/cheerio) 莫属了。其实 cheerio 应该算是 jQuery 的一个针对 DOM 操作优化和精简的子集,包含了 DOM 操作的大部分内容,去除了其它不必要的内容。使用 cheerio 你就可以像用普通 jQuery 选择器那样选择你需要的内容。
下载图片
在爬数据时,我们可能还需要下载图片。其实下载图片的方式和普通的网页没有太大的区别,但是有一点让我吃了苦头。
注意下面代码中言辞激烈的注释,那就是我年轻时犯下的错误……
var req = http.request(options, function(res){ //初始化数据!!! var binImage = ''; res.setEncoding('binary'); res.on('data', function(chunk){ binImage += chunk; }); res.on('end', function(){ if (!binImage) { console.log('image data is null'); return null; } fs.writeFile(imageFolder + filename, binImage, 'binary', function(err){ if (err) { console.log('image writing error:' + err.message); return null; } else{ console.log('image ' + filename + ' saved'); return filename; } }); }); res.on('error', function(e){ console.log('image downloading response error:' + e.message); return null; }); }); req.end();
GBK 转码
另外一个值得说明的问题就是 node.js 爬虫在爬 GBK 编码内容时转码的问题,其实这个问题很好解决,但是新手可能会绕弯路。这里就把源码全部奉上:
var req = http.request(options, function(res) { res.setEncoding('binary'); res.on('data', function (chunk) { html += chunk; }); res.on('end', function(){ //转换编码 html = iconv.decode(html, 'gbk'); }); }); req.end();
这里我使用的转码库是 iconv-lite(https://github.com/ashtuchkin/iconv-lite),完美支持 GBK 和 GB2312 等双字节编码。
实例:爬虫批量下载百度图片
var fs = require('fs'), path = require('path'), util = require('util'), // 以上为Nodejs自带依赖包 request = require('request'); // 需要npm install的包 // main函数,使用 node main执行即可 patchPreImg(); // 批量处理图片 function patchPreImg() { var tag1 = '摄影', tag2 = '国家地理', url = 'http://image.baidu.com/data/imgs?pn=%s&rn=60&p=channel&from=1&col=%s&tag=%s&sort=1&tag3=', url = util.format(url, 0, tag1, tag2), url = encodeURI(url), dir = 'D:/downloads/images/', dir = path.join(dir, tag1, tag2), dir = mkdirSync(dir); request(url, function(error, response, html) { var data = JSON.parse(html); if (data && Array.isArray(data.imgs)) { var imgs = data.imgs; imgs.forEach(function(img) { if (Object.getOwnPropertyNames(img).length > 0) { var desc = img.desc || ((img.owner && img.owner.userName) + img.column); desc += '(' + img.id + ')'; var downloadUrl = img.downloadUrl || img.objUrl; downloadImg(downloadUrl, dir, desc); } }); } }); } // 循环创建目录 function mkdirSync(dir) { var parts = dir.split(path.sep); for (var i = 1; i <= parts.length; i++) { dir = path.join.apply(null, parts.slice(0, i)); fs.existsSync(dir) || fs.mkdirSync(dir); } return dir; } var index = 1; // 开始下载图片,并log统计日志 function downloadImg(url, dir, desc) { var fileType = 'jpg'; if (url.match(/\.(\w+)$/)) fileType = RegExp.$1; desc += '.' + fileType; var options = { url: url, headers: { Host: 'f.hiphotos.baidu.com', Cookie: 'BAIDUID=810ACF57B5C38556045DFFA02C61A9F8:FG=1;' } }; var startTime = new Date().getTime(); request(options) .on('response', function() { var endTime = new Date().getTime(); console.log('Downloading...%s.. %s, 耗时: %ss', index++, desc, (endTime - startTime) / 1000); }) .pipe(fs.createWriteStream(path.join(dir, desc))); }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Java crawler practice: How to efficiently crawl web page data Introduction: With the rapid development of the Internet, a large amount of valuable data is stored in various web pages. To obtain this data, it is often necessary to manually access each web page and extract the information one by one, which is undoubtedly a tedious and time-consuming task. In order to solve this problem, people have developed various crawler tools, among which Java crawler is one of the most commonly used. This article will lead readers to understand how to use Java to write an efficient web crawler, and demonstrate the practice through specific code examples. 1. The base of the reptile

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Practical skills sharing: Quickly learn how to crawl web page data with Java crawlers Introduction: In today's information age, we deal with a large amount of web page data every day, and a lot of this data may be exactly what we need. In order to quickly obtain this data, learning to use crawler technology has become a necessary skill. This article will share a method to quickly learn how to crawl web page data with a Java crawler, and attach specific code examples to help readers quickly master this practical skill. 1. Preparation work Before starting to write a crawler, we need to prepare the following
