【问题标题】:Scrape part of page that is not html刮掉非 html 的页面部分
【发布时间】:2018-03-27 12:01:20
【问题描述】:

我想刮this site。 我将 Node.js 和 Phantom.js 与 Phantom 一起使用。 这是我的代码:

var phantom = require('phantom');

var loadInProgress = false;
var url = 'http://apps.who.int/flumart/Default?ReportNo=12';

(async function() {
    const instance = await phantom.create();
    const page = await instance.createPage();

    await page.on('onResourceRequested', function(requestData) {
        console.info('Requesting', requestData.url);
    });
    await page.on('onConsoleMessage', function(msg) {
        console.info(msg);
    });
    await page.on('onLoadStarted', function() {
        loadInProgress = true;
        console.log('Load started...');
    });
    await page.on('onLoadFinished', function() {
        loadInProgress = false;
        console.log('Load end');
    });

    const status = await page.open(url);
    await console.log('STATUS:', status);

    const content = await page.property('content');
    await console.log('CONTENT:', content);

    // submit
    await page.evaluate(function() {
        document.getElementById('lblFilteBy').value = 'Country, area or territory'; //'WHO region';
        document.getElementById('lblSelectBy').value = 'Italy'; //'European Region of WHO';
        document.getElementById('lbl_YearFrom').value = '1995';
        document.getElementById('lbl_WeekFrom').value = '1';
        document.getElementById('lbl_YearTo').value = '2018';
        document.getElementById('ctl_list_WeekTo').value = '53';
        //console.log('SUBMIT:', document.getElementById('ctl_ViewReport'));
        document.getElementById('ctl_ViewReport').submit();
    });

    var result = await page.evaluate(function() {
        return document.querySelectorAll('html')[0].outerHTML; // Problem here
    });
    await console.log('RESULT:', result);

    await instance.exit();
}());

我不明白页面的这部分(红色)是什么:

不是 HTML,如何抓取显示的数据?

谢谢!


编辑 1

如果我转到 Chrome 开发工具的“网络”标签:

【问题讨论】:

  • 看起来像是一个java小程序。 HTML 源代码可以告诉你它是什么类型的对象。
  • @EriksKlotins 谢谢,但你能更准确一点吗?
  • 您的 html 中的某处应该是 标签。查看属性,type 和 src 应该告诉你嵌入对象的类型/来源。
  • @EriksKlotins 我从 Chrome 中进行了 View page source 并查找了 embedobject。他什么也没找到……
  • 好的,我查看了网站。它是动态插入的 MS Report Viewer。您无法从 html 中抓取任何内容。

标签: node.js web-scraping phantomjs


【解决方案1】:

您可以捕获 请求,检查:

用蓝色标出,这是您需要在phantom 脚本中调用自己的 XHR 请求,而 ajax 结果用红色标出。在标题选项卡中,您将看到通过 POST 发送到页面的表单数据

【讨论】:

    【解决方案2】:

    这将很难。看看这个:Node.js web browser with JavaScript execution

    基本上,你需要一个模拟浏览器执行js的lib,并用它来呈现报告,然后你就可以解析它。

    【讨论】:

    • 非常感谢,所以 Phantom.js 对我的目标很有帮助。但我怎么能用它呢?我很困惑
    • 看起来你正在抓取的数据无论如何都是公共领域的。写信给世界卫生组织,礼貌地索要数据。这里没有什么可失去的。
    • 你认为phantom 是什么?这都是关于 js 能力的库,它是 phantomjs 的包装器
    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2017-08-15
    • 1970-01-01
    • 2014-04-02
    • 2014-03-14
    • 2014-02-08
    相关资源
    最近更新 更多