2016-10-10 26 views
1

我試圖使用casperjs自動導出媒體wiki xml,因爲我們沒有通過託管它的機器訪問。問題是下載xml作爲其響應是xml的發佈請求。目前,我有以下的(另一張機票已在堆棧溢出帶來的是這個代碼)CasperJS和從mediawiki自動導出xml

casper.then(function(){ 
     var theFormRequest = this.page.evaluate(function() { 
      var request = {}; 
      var formDom = document.forms[0]; 
      formDom.onsubmit = function() { 
       //iterate the form fields 
       var data = {}; 
       for(var i = 0; i < formDom.elements.length; i++) { 
       data[formDom.elements[i].name] = formDom.elements[i].value; 
      } 
      request.action = formDom.action; 
      request.data = data; 
      return false; //Stop form submission 
     } 

     var link = $(".visualClear").submit();  
     return request; 
    }); 


     this.echo("DOWNLOADING " + theFormRequest.action + " " + theFormRequest.data); 
     casper.download(theFormRequest.action, "downloaded_file.xml", "POST", theFormRequest.data); 
    }); 

我用resource.received事件,但這個只是給了我回應的元數據而不是實際的數據。目前下載的內容是html頁面而不是xml,我可以確認該按鈕正在被點擊,就好像我刪除了formDom.onSubmit resource.received,這表明content.type返回的是XML。

雖然它的不同之處,從我的mediawiki使用的類而言,這裏是MediaWiki的頁面做同樣的事情https://www.mediawiki.org/wiki/Special:Export

感謝

編輯 這裏是響應通過瀏覽器完成時

Accept-Ranges:bytes 
Age:0 
Connection:keep-alive 
Content-disposition:attachment;filename=file-20161010165904.xml 
Content-Length:172717 
Content-Type:application/xml; charset=utf-8 
Date:Mon, 10 Oct 2016 16:59:04 GMT 
Server:nginx 
Via:1.1 varnish 
X-Content-Type-Options:nosniff 
X-Powered-By:HHVM/3.15.1 
X-Varnish:1482412076 
+0

我不確定您的問題的答案,但如果您想要完成一個wiki的下載,您可能會對Wikiteam存檔工具感興趣:https://github.com/wikiTeam/wikiteam –

回答

0

我該做什麼:

casper.start(); 

casper.open('http://url_to_mediawiki', { 
     method: 'GET', 
     headers: { 
      'Content-Type': 'application/xml; charset=utf-8', 
     }, 
     encoding: 'utf8' 
    } 
}) 

casper.then(function() { 
    this.echo(this.getPageContent()); 
}) 

getPageContent()內容不可知,它很靈活。它只是呈現它找到的東西。檢查casper#getpagecontentcasper#open