抓取不是 html 的页面部分
Scrape part of page that is not html
我想刮this site。
我在 Phantom 中使用 Node.js 和 Phantom.js。
这是我的代码:
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 开发工具的 'Network' 选项卡:
这会很难。看看这个:
基本上,你需要一个lib来模拟一个带有js执行的浏览器,并用它来呈现报表,然后你就可以解析它了。
您可以捕获 ajax 请求,检查:
蓝色框出的是您需要在phantom
脚本中调用自己的XHR请求,红色框出的是ajax结果。在 header 选项卡中,您将看到 表单数据 通过 POST 发送到页面。
我想刮this site。 我在 Phantom 中使用 Node.js 和 Phantom.js。 这是我的代码:
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 开发工具的 'Network' 选项卡:
这会很难。看看这个:
基本上,你需要一个lib来模拟一个带有js执行的浏览器,并用它来呈现报表,然后你就可以解析它了。
您可以捕获 ajax 请求,检查:
蓝色框出的是您需要在phantom
脚本中调用自己的XHR请求,红色框出的是ajax结果。在 header 选项卡中,您将看到 表单数据 通过 POST 发送到页面。