Created
January 9, 2018 07:39
-
-
Save foru17/3bbc947cc014f882824db04adc67ef2c to your computer and use it in GitHub Desktop.
简单的百度搜索结果数量统计api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cheerio = require('cheerio'); | |
var http = require('http'); | |
var iconv = require('iconv-lite'); | |
var express = require('express'); | |
var app = express(); | |
app.get('/api/baidu/:keyword', function(req, res) { | |
var keyword = req.param('keyword'); | |
var encodeKey = encodeURIComponent(keyword); | |
var url = 'http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=' + encodeKey + '&rsv_pq=d1bc678500027fae&rsv_t=b1baqnanzIu6%2B%2B6jgA5UgJY9hPGjPUfG2I03drYiQCZLKs41hn1TBNqN2tg&rqlang=cn&rsv_enter=1&rsv_sug3=14&rsv_sug1=13&rsv_sug7=100&rsv_sug2=0&inputT=1230&rsv_sug4=1231' | |
http.get(url, function(sres) { | |
var chunks = []; | |
sres.on('data', function(chunk) { | |
chunks.push(chunk); | |
}); | |
sres.on('end', function() { | |
var html = iconv.decode(Buffer.concat(chunks), 'utf-8'); | |
var $ = cheerio.load(html, { | |
decodeEntities: false | |
}); | |
var searchResult = parseFloat($('.nums').html().split('百度为您找到相关结果约')[1].split('个')[0].trim().replace(/\,/g,'')); | |
res.send(JSON.stringify({ | |
keyword: keyword, | |
num: searchResult, | |
msg:$('.nums').html().split('搜索工具')[1] | |
}, null, 4)); | |
}); | |
}); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment