【问题标题】:Dynamic scraping using nodejs and phantomjs使用 nodejs 和 phantomjs 进行动态抓取
【发布时间】:2016-04-15 18:48:59
【问题描述】:

首先,我已经成功安装了 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”内,我应该如何将其他方法连接到页面,这还不是很清楚。

【问题讨论】:

    标签: javascript node.js web-scraping phantomjs


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      html 底部有 CData 脚本无法被 phantom 解析。这是从中传播项目的地方。

      <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>
      

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

      【讨论】:

        猜你喜欢
        • 2014-03-18
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-26
        • 1970-01-01
        相关资源
        最近更新 更多