获取html表单外部网站的一部分

我怎样才能得到一个网站sorce代码的考试部分,如果我想说的ony想要得到这部分的代码

<span class="definition">Används när man säger hejdå till någon. Ha de G är samma sak som ha det bra, eller ha det gött.</span><br /><br /> 

形成很多的代码,我想要在Javascript中做到这一点,代码是从外部网站,NAd这是node.js

你所说的一般技术叫做“网页抓取”,可以从简单到非常复杂。 假设你正在寻找节点应用程序(如你的标签所示),我build议使用模块requestcheerio 。 如果html的一部分是由客户端JavaScript生成的,这将是不够的,但你没有在你的问题中表明。 这是一个非常简单的代码片段,它描述了我所build议的内容:

 const cheerio = require('cheerio'); const request = require('request'); request.get('http://example.com/index.html', (err, response, body) => { const $ = cheerio.load(body); const definitions = $('span.definition'); console.log(definitions); // these are all selection result elements, you can do more with them here once you see what they contain. }); 

你可以使用cheerio

 const request = require("request"); const cheerio = require("cheerio"); request("http://example.com/some-uri", (err, response, body) => { if(err) throw err; //Handle error let $ = cheerio.load(body); let myElement = $('span.definition'); console.log(myElement.html()); //Inner html console.log($.html(myElement)); //outer html });