puppeteer 呼叫谷歌翻譯api

卢老师不想编程發表於2024-04-22
npm i puppeteer
index.js
const puppeteer = require('puppeteer');

async function translateText(text, sl, tl) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
`http://translate.google.com/?sl=${sl}&tl=${tl}&text=${text}&op=translate`
);
const bodyHandle = await page.$('.ryNqvb');
const html = await page.evaluate((body) => body.innerHTML, bodyHandle);
console.log('html', html);
await browser.close();
return html;
}

translateText('Hello, world!', 'auto', 'zh-CN');
node inde.js

相關文章