我有一个使用JavaScript创建的移动应用程序条形码扫描程序,并希望在网络扫描程序中使用UPC代码来获取有关该产品的信息。
刮刀可以从UPC数据库中获取视频游戏的标题。
const rp = require('request-promise');
const cheerio = require('cheerio');
const options = {
uri: `https://barcodeindex.com/upc/722674120708/`,
应在URL中输入UPC条形码而不是长号,这只是一个测试代码。
transform: function (body) {
return cheerio.load(body);
}
};
rp(options)
.then(($) => {
console.log($('#item-sub-title').text());
})
.catch((err) => {
console.log(err);
});
如果我想抓取视频游戏的标题,然后使用该标题抓取Metacritic.com获取有关视频游戏的信息,我该怎么做?或者甚至可能吗?
答案 0 :(得分:0)
是的,这是可能的。您可以使用像request
这样的http客户端库,并像这样使用它:
const request = require('request')
request('url.com', (error, response, body) => {
if (error) throw error
if (response && response.statusCode === 200) {
// Here we call your findVideogameTitle function, which searches for the
// videogame title enclosing tag and extracts the element text.
console.log(findVideogameTitle(body))
} else {
console.log(`Something happened: ${response.statusCode}`)
}
})
如果被抓取的页面是延迟加载的而不是服务器呈现的,那么您可能需要一个完整的无头浏览器来完成该任务,例如puppeteer
。它很容易使用,但会从CPU和内存中获取更多资源。