// 发送请求
const request = require('request')
// 处理 dom 结构
const cheerio = require('cheerio')
function Movie() {
this.name = ''
this.score = 0
this.quote = ''
this.ranking = 0
this.coverUrl = ''
}
const moviesFromUrl = function(url) {
request(url, function(error, response, body) {
if (error === null && response.statusCode == 200) {
log('*** SUCC 请求成功')
// ...
} else {
log('*** ERROR 请求失败', error)
}
})
}
const movieFromDiv = function(div) {
const movie = new Movie()
const e = cheerio.load(div)
movie.name = e('.title').text()
movie.score = e('.rating_num').text()
movie.quote = e('.inq').text()
const pic = e('.pic')
movie.ranking = pic.find('em').text()
movie.coverUrl = pic.find('img').attr('src')
return movie
}
const saveMovie = function(movies) {
const fs = require('fs')
// 可以改变的文件名
const path = 'douban.txt'
const s = JSON.stringify(movies, null, 2)
fs.writeFile(path, s, function(error) {
if (error !== null) {
log('*** 写入文件错误', error)
} else {
log('--- 保存成功')
}
})
}
const __main = function() {
const url = 'https://movie.douban.com/top250'
moviesFromUrl(url)
}
__main()