使用cheerio解析html

  1. cnpm i cheerio axios -S
  1. const koa = require("koa");
  2. const app = new koa();
  3. const Router = require("koa-router");
  4. const router = new Router();
  5. const axios = require("axios");
  6. const cheerio = require("cheerio");
  7. const url = "https://movie.douban.com/top250"
  8. router.get("/douban",async ctx=>{
  9. var html = await axios.get(url);
  10. const $ = cheerio.load(html.data,{
  11. decodeEntities:false //解码设置为false,中文不会乱码
  12. });
  13. var title = $("#content h1").html();
  14. console.log(title)
  15. ctx.body = title;
  16. })
  17. app.use(router.routes());
  18. app.listen(8080)
  19. /*
  20. 1.axios抓取豆瓣
  21. 2.cheerio解析页面
  22. 3.返回数据给前端
  23. */
  1. //http://www.kuwo.cn/playlists
  2. # 抓取酷我音乐 [{imgUrl:"xxx",name:"xxxx",playCount:xxx}]

接口文档

2-1 列表页

  1. //locahost?start=0 默认地址 默认获取25条,想去获取下一个只用更改start值就可以了
  2. //locahost?start=25

2-2 详情页的数据

  1. /subject/:id
  2. 前后端接口联调

2-3 搜索页的数据

  1. /search?keyword=value
  2. http://www.kuwo.cn/playlists
  3. 抓取酷我搜索页