2015-07-22 119 views
9

我想为本页上的每个链接追查页面后面的详细信息页面。从网站刮脸,用javascript:subOpen href链接

我能得到这个网页上的所有信息:PAGE

不过,我想获得的所有信息的详细信息页面上,但在href链接看起来像,例如:

href="javascript:subOpen('9ca8ed0fae15d43dc1257e7300345b99')" 

以下是使用ImportHTML函数获得总体概述的示例电子表格。

Google Spreadsheet

任何建议如何获得详细信息页面?

UPDATE

我采取了以下方法:

function doGet(e){ 
    var base = 'http://www.ediktsdatei.justiz.gv.at/edikte/ex/exedi3.nsf/' 
    var feed = UrlFetchApp.fetch(base + 'suche?OpenForm&subf=e&query=%28%5BVKat%5D%3DEH%20%7C%20%5BVKat%5D%3DZH%20%7C%20%5BVKat%5D%3DMH%20%7C%20%5BVKat%5D%3DMW%20%7C%20%5BVKat%5D%3DMSH%20%7C%20%5BVKat%5D%3DGGH%20%7C%20%5BVKat%5D%3DRH%20%7C%20%5BVKat%5D%3DHAN%20%7C%20%5BVKat%5D%3DWE%20%7C%20%5BVKat%5D%3DEW%20%7C%20%5BVKat%5D%3DMAI%20%7C%20%5BVKat%5D%3DDTW%20%7C%20%5BVKat%5D%3DDGW%20%7C%20%5BVKat%5D%3DGA%20%7C%20%5BVKat%5D%3DGW%20%7C%20%5BVKat%5D%3DUL%20%7C%20%5BVKat%5D%3DBBL%20%7C%20%5BVKat%5D%3DLF%20%7C%20%5BVKat%5D%3DGL%20%7C%20%5BVKat%5D%3DSE%20%7C%20%5BVKat%5D%3DSO%29%20AND%20%5BBL%5D%3D0').getContentText(); 

     var d = document.createElement('div'); //assuming you can do this 
     d.innerHTML = feed;//make the text a dom structure 
     var arr = d.getElementsByTagName('a') //iterate over the page links 
     var response = ""; 
     for(var i = 0;i<arr.length;i++){ 
     var atr = arr[i].getAttribute('onclick'); 
     if(atr) atr = atr.match(/subOpen\((.*?)\)/) //if onclick calls subOpen 
     if(atr && atr.length > 1){ //get the id 
      var detail = UrlFetchApp.fetch(base + '0/'+atr[1]).getContentText(); 
      response += detail//process the relevant part of the content and append to the reposnse text 
     } 
     }  
     return ContentService.createTextOutput(response); 
} 

然而,在运行方法时,我得到一个错误:

ReferenceError: "document" is not defined. (line 6, file "")

什么是document的对象?

我用webapp更新了Google Spreadsheet

我感谢您的回复!

+0

[此问题]的副本(http://stackoverflow.com/questions/31452272/google-app-script-urlfetch-not-giving-html-output-but-javascript-ouput)。 – Mogsdad

回答

6

您可以使用Firebug来检查页面内容和JavaScript。例如,你可以发现subOpen实际上是在xmlhttp01.js中声明的subOpenXML的别名。

function subOpenXML(unid) {/*open found doc from search view*/ 
if (waiting) return alert(bittewar); 
var wState = dynDoc.getElementById('windowState'); 
wState.value = 'H';/*httpreq pending*/ 
var last = ''; 
if (unid==docLinks[0]) {last += '&f=1'; thisdocnum = 1;} 
if (unid==docLinks[docLinks.length-1]) { 
    last += '&l=1'; 
    thisdocnum = docLinks.length; 
} else { 
    for (var i=1;i<docLinks.length-1;i++) 
    if (unid==docLinks[i]) {thisdocnum = i+1; break;} 
} 
var url = unid + html_delim + 'OpenDocument'+last + '&bm=2'; 
httpreq.open('GET', // &rand=' + Math.random(); 
    /*'/edikte/test/ex/exedi31.nsf/0/'+*/ '0/'+url, true); 
httpreq.onreadystatechange=onreadystatechange; 
// httpreq.setRequestHeader('Accept','text/xml'); 
httpreq.send(null); 
waiting = true; 
title2src = firstTextChild(dynDoc.getElementById('title2')).nodeValue; 
} 

所以,复制功能的源和Firebug的控制台选项卡在修改之后的HTTP调用前添加console.log(url),像这样:

var url = unid + html_delim + 'OpenDocument'+last + '&bm=2'; 
console.log(url) 
httpreq.open('GET', // &rand=' + Math.random(); 
    /*'/edikte/test/ex/exedi31.nsf/0/'+*/ '0/'+url, true); 

可以在Firebug的控制台选项卡执行的函数声明并用修改的源码覆盖子打开。 Clickin中的链接,然后将显示该调用的网址是由作为参数传递的ID来subOpen“0 /”前缀,所以在这个例子中你张贴这将是一个GET到:

http://www.ediktsdatei.justiz.gv.at/edikte/ex/exedi3.nsf/0/1fd2313c2e0095bfc1257e49004170ca?OpenDocument&f=1&bm=2 

你也可以通过打开萤火虫中的网络标签并点击链接来验证。

因此,为了凑详细信息页面你需要

  1. 解析传递给subOpen
  2. 进行GET呼叫ID为“0 /”
  3. 解析请求响应

在firebug的Network Tab中查看请求响应表明可能需要执行类似的解析才能真正获得显示的内容,但我没有深入研究它。

UPDATE importHTML函数并不适合你想要的那种抓取。 Google的HTMLContent服务更适合于此。你需要创建一个web app并实现doGet功能:

function doGet(e){ 
    var base = 'http://www.ediktsdatei.justiz.gv.at/edikte/ex/exedi3.nsf/' 
    var feed = UrlFetchApp.fetch(base + 'suche?OpenForm&subf=e&query=%28%5BVKat%5D%3DEH%20%7C%20%5BVKat%5D%3DZH%20%7C%20%5BVKat%5D%3DMH%20%7C%20%5BVKat%5D%3DMW%20%7C%20%5BVKat%5D%3DMSH%20%7C%20%5BVKat%5D%3DGGH%20%7C%20%5BVKat%5D%3DRH%20%7C%20%5BVKat%5D%3DHAN%20%7C%20%5BVKat%5D%3DWE%20%7C%20%5BVKat%5D%3DEW%20%7C%20%5BVKat%5D%3DMAI%20%7C%20%5BVKat%5D%3DDTW%20%7C%20%5BVKat%5D%3DDGW%20%7C%20%5BVKat%5D%3DGA%20%7C%20%5BVKat%5D%3DGW%20%7C%20%5BVKat%5D%3DUL%20%7C%20%5BVKat%5D%3DBBL%20%7C%20%5BVKat%5D%3DLF%20%7C%20%5BVKat%5D%3DGL%20%7C%20%5BVKat%5D%3DSE%20%7C%20%5BVKat%5D%3DSO%29%20AND%20%5BBL%5D%3D0').getContentText(); 
     var response = ""; 
     var match = feed.match(/subOpen\('.*?'\)/g) 
     if(match){ 
     for(var i = 0; i < match.length;i++){ 
       var m = match[i].match(/\('(.*)'\)/); 
       if(m && m.length > 1){ 
       var detailText = UrlFetchApp.fetch(base + '0/'+m[1]); 
       response += //dosomething with detail text 
          //and concatenate in the response 
       } 
     } 
     } 
     return ContentService.createTextOutput(response); 


} 
+0

Thx为您的答案!我不得不说,我对谷歌脚本语言并不太熟悉,因此,如果能够帮助我在谷歌电子表格中显示我想要的内容,我将不胜感激。我会自己解析解析。 – mrquad

+1

刚刚更新了答案 – Grasshopper

+0

Thx为您的答案!但是,我仍然遇到错误,请查看我的更新! – mrquad

-1

如果你有Java的背景位,你可以使用http://htmlunit.sourceforge.net/(或其他测试框架)放弃任何种类的网页。它支持Java脚本交互以及实用程序方法来获取网页元素。