現在的網頁裡,不可避免的會放入大量的javascript,而這些javascript會在使用者連入網頁時,由使用者所用的瀏覽器自行執行後再顯示.

有注意到嗎??這些javascript在網站上是javascript,到了使用者的電腦才由電腦裡的瀏覽器變成內容的.

所以你今天如果想要把網頁內容從網站上直接下載下來,你就會發現下載的網頁殘缺不全

那要怎麼解決這個問題呢??

節錄了這個網址的解決方案 http://johnson560.pixnet.net/blog/post/322219893

如何下載javascript的完整網頁內容

這裡有一個簡單的小工具phantomjs script可以完成
phantomjs script可以觸發網頁的javascript並且允許你下載成html檔案

Here is a simple little phantomjs script that triggers javascript on a webpage and allows you to pull it down locally: 

file: get.js 
var page = require('webpage').create(),
  system = require('system'), address;

address = system.args[1];
page.scrollPosition= { top: 4000, left: 0}  
page.open(address, function(status) {
  if (status !== 'success') {
    console.log('** Error loading url.');
  } else {
    console.log(page.content);
  }
  phantom.exit();
});

Use it as follows: 
$> phantomjs /path/to/get.js "http://www.google.com" > "google.html" 

Changing /path/to, url and filename to what you want.

 

arrow
arrow

    痞客興 發表在 痞客邦 留言(0) 人氣()