Closed annewanghy closed 6 years ago
why@why-PC:~/Downloads$ node
bash: node: 未找到命令
why@why-PC:~/Downloads$ nvm use stable
Now using node v6.8.0 (npm v3.10.8)
使用eventProxy
var eventproxy = require('eventproxy')
var superagent = require('superagent')
var cheerio = require('cheerio')
var url = require('url')
var cnodeUrl = 'https://cnodejs.org'
superagent.get(cnodeUrl)
.end(function(err, res){
if(err) {
return next(err)
}
var topicUrls = []
// 使用cheerio解析网页的html内容,与jquery类似
var $ = cheerio.load(res.text)
var items = []
$('#topic_list .topic_title').each(function(idx, element) {
var $element = $(element)
var href = url.resolve(cnodeUrl, $element.attr('href'))
topicUrls.push(href)
})
console.log(topicUrls)
})
[ 'https://cnodejs.org/topic/597edd7f8f0313ff0d08d97a',
'https://cnodejs.org/topic/58d0fb3517f61387400b7e15',
'https://cnodejs.org/topic/58eee565a92d341e48cfe7fc',
'https://cnodejs.org/topic/58ad76db7872ea0864fedfcc',
'https://cnodejs.org/topic/592917b59e32cc84569a7458',
'https://cnodejs.org/topic/5983dc3607cef83a61770a39',
'https://cnodejs.org/topic/5983c6865f8478756117f705',
'https://cnodejs.org/topic/57d68794cb6f605d360105bf',
'https://cnodejs.org/topic/5981cbde07cef83a617709ab',
'https://cnodejs.org/topic/594f6e21642874f845d9fe0d',
'https://cnodejs.org/topic/5982ec5b07cef83a61770a08',
'https://cnodejs.org/topic/5983c45b5f8478756117f703',
'https://cnodejs.org/topic/5982c1fdc1eb855961dd60ef',
'https://cnodejs.org/topic/5981b0db28607f916122dbd7',
'https://cnodejs.org/topic/59827b8e5f8478756117f6ae',
'https://cnodejs.org/topic/59813c92518c054e4fc0c377',
'https://cnodejs.org/topic/5982e22a07cef83a61770a01',
'https://cnodejs.org/topic/5982dd41c1eb855961dd610b',
'https://cnodejs.org/topic/5982d6755f8478756117f6dd',
'https://cnodejs.org/topic/5941f4959079357b642b2782',
'https://cnodejs.org/topic/5982833d07cef83a617709d1',
'https://cnodejs.org/topic/5982c924c1eb855961dd60f7',
'https://cnodejs.org/topic/58caaec27dee71e5193a53ce',
'https://cnodejs.org/topic/59813d88518c054e4fc0c37a',
'https://cnodejs.org/topic/596c635927c8372819db0b84',
'https://cnodejs.org/topic/59829644c1eb855961dd60ec',
'https://cnodejs.org/topic/5981d74fc1eb855961dd60c1',
'https://cnodejs.org/topic/597ca460e72077ff35fbbe7d',
'https://cnodejs.org/topic/598132bd8f0313ff0d08da42',
'https://cnodejs.org/topic/598267f307cef83a617709c5',
'https://cnodejs.org/topic/59827e8428607f916122dc23',
'https://cnodejs.org/topic/598271395f8478756117f6a9',
'https://cnodejs.org/topic/5982728cc1eb855961dd60d7',
'https://cnodejs.org/topic/57c68052b4a3bca66bbddbdd',
'https://cnodejs.org/topic/5982791728607f916122dc1d',
'https://cnodejs.org/topic/598045ec68aa87c774e5ee1e',
'https://cnodejs.org/topic/5981ee8e07cef83a617709be',
'https://cnodejs.org/topic/597995a5e72077ff35fbbd9e',
'https://cnodejs.org/topic/57ce8a5e8039ce12476979af',
'https://cnodejs.org/topic/598136bc8f0313ff0d08da48' ]
处理异步 eventproxy 提供了不少其他场景所需的 API,但最最常用的用法就是以上的这种,即:
var eventproxy = require('eventproxy')
var superagent = require('superagent')
var cheerio = require('cheerio')
var url = require('url') var cnodeUrl = 'https://cnodejs.org' var topicUrls = []
superagent.get(cnodeUrl) .end(function(err, res){ if(err) { return next(err) } var topicUrls = []
// 使用cheerio解析网页的html内容,与jquery类似
var $ = cheerio.load(res.text)
var items = []
$('#topic_list .topic_title').each(function(idx, element) {
var $element = $(element)
var href = url.resolve(cnodeUrl, $element.attr('href'))
topicUrls.push(href)
})
console.log(topicUrls)
var ep = new eventproxy()
ep.after('topic_html', topicUrls.length, function(topics){
// 开始行动
topics = topics.map(function(topicPair){
var topicUrl = topicPair[0]
var topicHtml = topicPair[1]
var $1 = cheerio.load(topicHtml)
return ({
title: $1('.topic_full_title').text().trim(),
href: topicUrl,
comment1: $1('.reply_content').eq(0).text().trim()
})
})
console.log('final: ')
console.log(topics)
})
topicUrls.forEach(function(topicUrl){
superagent.get(topicUrl)
.end(function(err, res){
console.log('fetch '+topicUrl +' successful')
ep.emit('topic_html', [topicUrl, res.text])
})
})
})
npm的替代品ied,like npm, but run faster
安装deepin系统(符合国人用户习惯的linux系统)
制作u盘启动盘
重启,按f12进入bash, 切换到startup启动项,将制作好的u盘调到首位
进入deepin安装界面, 选择安装盘
拔掉u盘, 重启, 完成安装
在deepin (linux)系统安装node.js 试了很多种方法, 最后发现nvm安装是最方便的 查看node.js的发行版本
安装完成后,重启bash, 你的 shell 里面应该就有个 nvm 命令了,调用它试试 $ nvm 当看到有输出时,则 nvm 安装成功。
安装 Node.js 使用 nvm 的命令安装 Node.js 最新稳定版,现在是 v6.8.0。
$ nvm install 6.8.0 安装完成后,查看一下 why@why-PC:~/Downloads$ node -v v6.8.0 why@why-PC:~/Downloads$ npm -v 3.10.8 查看自己安装的所有 Node.js 版本 $ nvm ls -> v6.8.0 node -> stable (-> v6.8.0) (default) stable -> 6.8 (-> v6.8.0) (default) iojs -> iojs- (-> N/A) (default) #iojs是node.js的一个预发版本, 可以自己百度查看
why@why-PC:~/Downloads$ node bash: node: 未找到命令 why@why-PC:~/Downloads$ nvm use stable Now using node v6.8.0 (npm v3.10.8)
npm config set registry 'https://registry.npm.taobao.org'
npm install -g cnpm
url