【发布时间】:2014-10-30 11:44:54
【问题描述】:
我想知道是否有类似 Scrapy for nodejs 的东西?如果不是,您如何看待使用简单页面下载并使用cheerio 对其进行解析?有没有更好的办法。
【问题讨论】:
标签: javascript node.js web-scraping scrapy cheerio
我想知道是否有类似 Scrapy for nodejs 的东西?如果不是,您如何看待使用简单页面下载并使用cheerio 对其进行解析?有没有更好的办法。
【问题讨论】:
标签: javascript node.js web-scraping scrapy cheerio
Scrapy 是一个为 python 添加异步 IO 的库。我们没有类似节点的原因是因为所有 IO 已经是异步的(除非你不需要它)。
这是一个scrapy脚本在节点中的样子,并注意url是同时处理的。
const cheerio = require('cheerio');
const axios = require('axios');
const startUrls = ['http://www.google.com/', 'http://www.amazon.com/', 'http://www.wikipedia.com/']
// this might be called a "middleware" in scrapy.
const get = async url => {
const response = await axios.get(url)
return cheerio.load(response.data)
}
// this too.
const output = item => {
console.log(item)
}
// here is parse which is the initial scrapy callback
const parse = async url => {
const $ = await get(url)
output({url, title: $('title').text()})
}
// and here is the main execution
startUrls.map(url => parse(url))
【讨论】:
我还没有见过像 Python 中的 Scrapy 这样强大的爬取/索引整个网站的解决方案,所以我个人使用 Python Scrapy 来爬取网站。
但是为了从页面中抓取数据,nodejs 中有 casperjs。这是一个非常酷的解决方案。它也适用于 ajax 网站,例如angular-js 页面。 Python Scrapy 无法解析 ajax 页面。 因此,对于抓取一页或几页的数据,我更喜欢使用 CasperJs。
Cheerio 确实比 casperjs 快,但它不适用于 ajax 页面,而且它没有像 casperjs 这样好的代码结构。所以即使你可以使用cheerio包,我也更喜欢casperjs。
咖啡脚本示例:
casper.start 'https://reports.something.com/login', ->
this.fill 'form',
username: params.username
password: params.password
, true
casper.thenOpen queryUrl, {method:'POST', data:queryData}, ->
this.click 'input'
casper.then ->
get = (number) =>
value = this.fetchText("tr[bgcolor= '#AFC5E4'] > td:nth-of-type(#{number})").trim()
【讨论】:
完全一样的吗?不,但既强大又简单?是的:crawler 快速示例:
var Crawler = require("crawler");
var c = new Crawler({
maxConnections : 10,
// This will be called for each crawled page
callback : function (error, res, done) {
if(error){
console.log(error);
}else{
var $ = res.$;
// $ is Cheerio by default
//a lean implementation of core jQuery designed specifically for the server
console.log($("title").text());
}
done();
}
});
// Queue just one URL, with default callback
c.queue('http://www.amazon.com');
// Queue a list of URLs
c.queue(['http://www.google.com/','http://www.yahoo.com']);
// Queue URLs with custom callbacks & parameters
c.queue([{
uri: 'http://parishackers.org/',
jQuery: false,
// The global callback won't be called
callback: function (error, res, done) {
if(error){
console.log(error);
}else{
console.log('Grabbed', res.body.length, 'bytes');
}
done();
}
}]);
// Queue some HTML code directly without grabbing (mostly for tests)
c.queue([{
html: '<p>This is a <strong>test</strong></p>'
}]);
【讨论】:
一些爬取功能可以通过Google Puppeteer 实现。根据文档:
您可以在浏览器中手动执行的大多数操作都可以使用Puppeteer 完成!以下是一些帮助您入门的示例:
【讨论】:
以防万一您仍然需要答案, https://www.npmjs.org/package/scrapy 我从未测试过它,但认为它可以提供帮助。 愉快的报废。
【讨论】: