js菜鸟在这里,
我正在尝试创建一个网络抓取工具,以从预订网站上抓取价格数据,但我无法获得所需的数据,至少不是每次都可以。
我正在测试此特定网址:
这是我在20次尝试中得到1的结果:
{ prices:
[ 'Prix / nuit',
'Hébergement',
'Avis',
'Emplacement',
'Autres',
'max. 500€+',
'Bien',
'108€',
'Bien',
'112€',
'Excellent',
'98€',
'Très bien',
'122€',
'Très bien',
'164€',
'Excellent',
'156€',
'Très bien',
'97€',
'Très bien',
'160€',
'Très bien',
'155€',
' ',
'87€',
'Excellent',
'134€',
'Très bien',
'155€',
' ',
'92€',
'Excellent',
'135€',
'Très bien',
'135€',
'Excellent',
'94€',
' ',
'82€',
'Très bien',
'98€',
'Excellent',
'99€',
'Bien',
'110€',
'Bien',
'141€',
' ',
'80€',
'Très bien',
'136€',
'Excellent',
'122€',
'Excellent',
'232€',
'1',
'trivago N.V.' ] }
这是我大部分时间得到的:
{ prices:
[ 'Prix / nuit',
'Hébergement',
'Avis',
'Emplacement',
'Autres',
'max. 500€+',
'trivago N.V.' ] }
有人告诉我,这可能与收集数据的速度有关,代码将在检索所有数据之前结束运行
代码:
const puppeteer = require('puppeteer');
let cheerio = require('cheerio');
let jsonframe = require('jsonframe-cheerio');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port);
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
let frame;
await page.goto('https://www.trivago.fr/?aDateRange%5Barr%5D=2019-10-09&aDateRange%5Bdep%5D=2019-10-10&aPriceRange%5Bfrom%5D=0&aPriceRange%5Bto%5D=0&iRoomType=7&aRooms%5B0%5D%5Badults%5D=2&cpt2=22748%2F200&iViewType=0&bIsSeoPage=0&sortingId=1&slideoutsPageItemId=&iGeoDistanceLimit=20000&address=&addressGeoCode=&offset=0&ra=');
let bodyHTML = await page.evaluate(() => document.body.innerHTML).then(frame = {"prices": ["strong"]});
let $ = cheerio.load(bodyHTML);
jsonframe($);
var postsList = $('body').scrape(frame);
console.log(postsList);
await browser.close();
})();
https://cdn-hs-graphql-dus.trivago.com/graphql
。您可以使用JSON解析器来解析响应,因此如果您不想使用Puppeteer进行解析,则应该了解它。[itemtype=""]
和[itemprop=""]
属性,因为此选择器可靠且快速找到。 https://css-tricks.com/how-css-selectors-work/console.log
,但是如果您更喜欢使用node.js作为服务器,我建议您使用express。jsonframe($)
之间的链接(async () => {
const http = require('http');
const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
const jsonframe = require('jsonframe-cheerio');
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
page.setRequestInterception(true);
page.on('request', async request => {
if ( request.resourceType() === 'image' || request.resourceType() === 'media' ) {
request.abort();
} else {
request.continue();
}
});
const response = await page.goto('https://www.trivago.fr/?aDateRange%5Barr%5D=2019-10-09&aDateRange%5Bdep%5D=2019-10-10&aPriceRange%5Bfrom%5D=0&aPriceRange%5Bto%5D=0&iRoomType=7&aRooms%5B0%5D%5Badults%5D=2&cpt2=22748%2F200&iViewType=0&bIsSeoPage=0&sortingId=1&slideoutsPageItemId=&iGeoDistanceLimit=20000&address=&addressGeoCode=&offset=0&ra=');
const waitForAJAXComplete = await page.waitForSelector('h3[itemprop="name"]>span.item-link');
const bodyHTML = await page.content();
const exit = await browser.close();
const $ = await cheerio.load(bodyHTML);
jsonframe($)
let frame = {
hotels : {
_s : "[itemtype='https://schema.org/Hotel']",
_d : [{
"hotelname" : "h3[itemprop='name']>span.item-link",
"hotelprice": "meta[itemprop='price'] ~ em ~ strong"
}]
}
};
const displayResult = $('body').scrape(frame, { string: true } );
if (displayResult.length > 0) {
const responseCode = '200';
const server = http.createServer(function(req,res){
res.writeHead(responseCode, {'Content-Type': 'application/json; charset=utf-8'});
res.end(displayResult);
}).listen(3000);
console.log('Node Server running on localhost:3000');
} else {
console.log('Node Server cannot running, because results cannot be parsed.')
}
})();