欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Node.js有道翻译爬虫

程序员文章站 2022-03-15 20:53:44
Node.js有道翻译爬虫环境node.js v12.16.3axios@0.20.0cheerio@1.0.0-rc.3npm安装npm install axios cheerio实现代码const cheerio = require("cheerio");const axios = require("axios");axios.get(`http://www.youdao.com/w/eng/${process.argv.splice(2)[0]}`).then((respon...

Node.js有道翻译爬虫

环境

node.js v12.16.3
axios@0.20.0
cheerio@1.0.0-rc.3

npm安装

npm install axios cheerio

实现代码

const cheerio = require("cheerio");
const axios = require("axios");

axios.get(`http://www.youdao.com/w/eng/${process.argv.splice(2)[0]}`).then((response) => {
    let $ = cheerio.load(response.data);
    $(".results-content").each((index, element) => {
        console.log(String($(".trans-container").children().first().text()).replace(' ', '').replace('\n', ''))
    });

  });

命令行参数

输入的第一个参数为要翻译的英文

node .\Translator.js apple

测试结果

    n. 苹果,苹果树,苹果似的东西;[美俚]炸弹,手榴弹,(棒球的)球;[美俚]人,家伙。

最后

  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!

本文地址:https://blog.csdn.net/qq_44486439/article/details/108842237