使用nodejs和phantomjs进行动态抓取

时间:2016-04-15 18:49:00

标签: javascript node.js web-scraping phantomjs

首先,我已成功安装了PhantomJs及其npm接口phantom。我已经设置了使用新语法加载我的页面的代码(此处发布的所有其他问题都基于旧的代码语法或者我遗漏了一些东西)。 this is the source I'm trying to scrape

现在,右侧边栏,一个在“Comune”附近有假选择的那个,另一个是动态生成的,我无法理解为什么phantomjs没有拿起它们。按照我的代码:

var sito = "http://bicincitta.tobike.it/";
var sitepage = null;
var phInstance = null;
var phantom = require('phantom')

phantom.create()
    .then((instance) => {
    phInstance = instance;
return instance.createPage();
})
.then((page) => {
    sitepage = page;
return page.open(sito);
})
.then((status) => {
    console.log(status);
return sitepage.property('content');
})
.then((content) => {
    console.log(content);
sitepage.close();
phInstance.exit();
})
.catch((error) => {
    console.log(error);
phInstance.exit();
})

我现在正在墙上用力撞击我的头。我应该以某种方式获取网站的脚本并执行它们吗?我错过了一条指令吗?

另外,在旁注;如果页面的作用域位于第二个“.then”内,我不应该如何将其他方法连接到页面上。

2 个答案:

答案 0 :(得分:1)

我过去一周与PhantomJS合作,试图让它用一个用角度渲染的数据对页面进行快照。我发现最简单的事情是对任何本地脚本使用page.injectJs('../script.js'),对任何外部脚本使用page.includeJs('http://jquery.com...')。由于Phantom是沙箱,它不会在它捕获的页面上执行javascript,除非你给它执行JS。这将允许您截取包含使用javascript呈现的数据的页面。

答案 1 :(得分:1)

html底部有CData脚本无法通过幻像解析。这是项目的传播地点。

<script type="text/javascript">
//<![CDATA[
Sys.Application.initialize();
Sys.Application.add_init(function() {
    $create(Telerik.Web.UI.RadAjaxManager, {"_updatePanels":"","ajaxSettings":[],"clientEvents":{OnRequestStart:"",OnResponseEnd:""},"defaultLoadingPanelID":"","enableAJAX":true,"enableHistory":false,"links":[],"styles":[],"uniqueID":"RadAjaxManager1","updatePanelsRenderMode":0}, null, null, $get("RadAjaxManager1"));
});
Sys.Application.add_init(function() {
    $create(Telerik.Web.UI.RadAjaxPanel, {"clientEvents":{OnRequestStart:"",OnResponseEnd:""},"enableAJAX":true,"enableHistory":false,"links":[],"loadingPanelID":"","styles":[],"uniqueID":"ajCheckLoginUser"}, null, null, $get("ajCheckLoginUser"));
});
Sys.Application.add_init(function() {
    $create(Telerik.Web.UI.RadAjaxPanel, {"clientEvents":{OnRequestStart:"",OnResponseEnd:""},"enableAJAX":true,"enableHistory":false,"links":[],"loadingPanelID":"","styles":[],"uniqueID":"ajCheckLoginAdmin"}, null, null, $get("ajCheckLoginAdmin"));
});
Sys.Application.add_init(function() {
    $create(Telerik.Web.UI.RadAjaxPanel, {"clientEvents":{OnRequestStart:"",OnResponseEnd:""},"enableAJAX":true,"enableHistory":false,"links":[],"loadingPanelID":"","styles":[],"uniqueID":"ajLogoutUser"}, null, null, $get("ajLogoutUser"));
});
Sys.Application.add_init(function() {
    $create(Telerik.Web.UI.RadWindow, {"_dockMode":false,"behaviors":0,"clientStateFieldID":"radPortal_ClientState","destroyOnClose":true,"formID":"form1","height":"180px","iconUrl":"","left":"","minimizeIconUrl":"","modal":true,"name":"radPortal","reloadOnShow":true,"showContentDuringLoad":false,"skin":"Office2007","top":"","visibleStatusbar":false,"width":"450px"}, {"close":OnClientClosePortal}, null, $get("radPortal"));
});
Sys.Application.add_init(function() {
    $create(Telerik.Web.UI.RadWindowManager, {"behaviors":4,"clientStateFieldID":"windowManagerPortal_ClientState","destroyOnClose":true,"formID":"form1","iconUrl":"","left":"","minimizeIconUrl":"","modal":true,"name":"windowManagerPortal","reloadOnShow":true,"showContentDuringLoad":false,"skin":"Office2007","top":"","visibleStatusbar":false,"windowControls":"['radPortal']"}, null, {"child":"radPortal"}, $get("windowManagerPortal"));
    });
//]]>
</script>

一旦您与此站点服务器保持通信,这些项目也将被销毁。有方法可以解决这个问题,但我认为你最好尝试别的东西。我使用npm cheerio加载CDATA html