Node.js有道翻译爬虫

2022-07-29,,,,

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

《Node.js有道翻译爬虫.doc》

下载本文的Word格式文档,以方便收藏与打印。