首页 > 解决方案 > 使用隐藏元素抓取动态网站

问题描述

目的是抓取booking.com以显示所有价格。为此,我使用 PhantomJS 运行下面的 javascript 代码,然后将网页的内容写入本地 html 文件。

var url = 'https://www.booking.com/searchresults.html?ss=Copenhagen%20Denmark&checkin_year=2020&checkin_month=2&checkin_monthday=6&checkout_year=2020&checkout_month=2&checkout_monthday=9&group_adults=2&group_children=0&no_rooms=1&no_dorms=1&nflt=pri%3D1%3Bpri%3D2%3B';
var page = require('webpage').create();
var fs = require('fs');

page.onLoadFinished = function() {
    fs.write('result.html', page.content, 'w');
    phantom.exit();
};

page.open(url, function() {
    page.evaluate(function() {
    });
});

问题是没有读取价格,结果看起来像这样(所有过滤器都丢失了,控制台中记录了一些警告和错误)

结果

例如,以下是一些应该被抓取的元素:

<div class="bui-price-display__value prco-inline-block-maker-helper" aria-hidden="true">
    €&amp;nbsp;246
</div>
<span class="bui-u-sr-only">
    Preis
    €&amp;nbsp;246
</span>

如果 PhantomJS 无法做到这一点,还有其他选择吗?我也尝试过 cURL 但无济于事。

标签: javascriptweb-scrapingphantomjs

解决方案


推荐阅读