2012-02-29 61 views
2

我想弄清楚WATIR。 這是一種情況。我想在幾個網站上監控廣告,但是抓取它們並不容易,因爲它們在iframe中,然後還有另一個由javascript生成的iframe鏈接。只有這樣我才能得到這個頁面。iframe中的Javascript。刮watir

這裏是主要頁面的代碼:

<iframe width="300" height="250" scrolling="no" frameborder="0" 
id="adbottomleft" src="/ad/left1" name="adbottomleft"></iframe> 

這裏是iframe中說:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<style type="text/css"> 

body { 
    background-color: black; 
    margin:0; 
    padding:0; 
}</style> 
</head> 
<body> 
<!-- Rubicon Project Tag --> 
<!-- Site: MangaReader Zone: ROS_BTF_LEFT Size: Medium Rectangle --> 
<div id="adfooter" style="width:300px;height:250px;"></div> 
<script language="JavaScript" type="text/javascript"> 
function tl(){ 
    var loaded = 0; 
    try { 
     loaded = parent.document['adver']; 
    } catch(e) { loaded = 0; } 
    if (loaded != 1) { 
     setTimeout(tl, 25); 
    } else { 
      var dest = document.getElementById('adfooter'); 
      var lframe = document.createElement('iframe'); 
      lframe.setAttribute('id','adbleft'); 
      lframe.setAttribute('width','300'); 
      lframe.setAttribute('height','250'); 
      lframe.setAttribute('scrolling','no'); 
      lframe.setAttribute('frameborder', '0'); 
      lframe.setAttribute('src', 'http://ad.mangareader.net/btleft1'); 
      dest.appendChild(lframe); 
    } 
} 
(function(){ 
tl(); 
}()); 
</script> 
</body> 
</html> 

它確實產生另一個iframe中,看起來像這樣:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<style type="text/css"> 
       * { 
      margin:0; 
      padding:0; 
     } 
     body { 
      margin-left: 0px; 
      margin-top: 0px; 
     } 
     </style> 
</head> 
<body> 
<!-- Rubicon Project Tag --> 
<!-- Site: MangaReader Zone: ROS_BTF_LEFT Size: Medium Rectangle --> 
<script language="JavaScript" type="text/javascript"> 
var cb = Math.random(); 
var d = document; 
var iframe = "&fr=" + (window != top); 
var ref = ""; 
try { 
    if (window != top) { 
     ref = "&rf="+escape(d.referrer); 
    } 
} catch (ignore) { } 
d.write("<iframe id='25504.15' name='25504.15' src='' framespacing='0' frameborder='no' scrolling='no' align='middle' width='300' height='250' marginheight='0' marginwidth='0'></iframe>"); 
d.getElementById('25504.15').src='http://optimized-by.rubiconproject.com/a/8240/13310/25504-15.html?cb='+cb+ref; 
</script> 
</body> 
</html> 

只有這樣,我纔有興趣刮掉最後一頁。

<html> 
    <head> 
    <meta http-equiv="Pragma" content="no-cache"> 
    <meta http-equiv="expires" content="0"> 
    <style type="text/css"> body {margin:0px; padding:0px;} </style> 
    <script type="text/javascript"> 
     rubicon_cb = Math.random(); rubicon_rurl = document.referrer; if(top.location==document.location){rubicon_rurl = document.location;} rubicon_rurl = escape(rubicon_rurl); 
     window.rubicon_ad = "3260765" + "." + "js"; 
     window.rubicon_creative = "3299047" + "." + "js"; 
    </script> 
    </head> 
    <body> 

<a href="http://optimized-by.rubiconproject.com/t/8240/13310/25504-15.3260765.3299047?url=http%3A%2F%2Fwww.animepremium.net" target="_blank"><img src="http://assets.rubiconproject.com/campaigns/100/91/16/5/1325630095ap_300.jpg" border="0" alt="AnimePremium.net" /></a><script defer="defer" type="text/javascript"> 
{ 
    if (Math.floor(Math.random()*100) < 1) 
    { 
     var url; 
     var iframe = (window != top); 
     url = "http://tap.rubiconproject.com/stats/iframes?pc=8240/13310&ptc=25504&upn="+iframe; 
     setTimeout(function(){ new Image().src = url }, 1000); 
    } 
} 
</script> 
<script>var _comscore = _comscore || []; _comscore.push({ c1: "8", c2: "6135404", c3: "28", c4: "13310", c10: "3299047" }); (function() { var s = document.createElement("script"), el = document.getElementsByTagName("script")[0]; s.async = true; s.src = (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/beacon.js"; el.parentNode.insertBefore(s, el); })();</script><DIV STYLE="height:0px; width:0px; overflow:hidden"><IFRAME SRC="http://tap2-cdn.rubiconproject.com/partner/scripts/rubicon/emily.html?rtb_ext=1&pc=8240/13310&geo=eu" FRAMEBORDER="0" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="NO" WIDTH="0" HEIGHT="0" style="height:0px; width:0px"></IFRAME></DIV> 
    </body> 
</html> 

不可能的任務?

這是我正在做的。

irb 
require "watir-webdriver" 
browser = Watir::Browser.new :ff 
browser.goto "mangareader.net" 
browser.frame(:id, "adbottomleft").html - Works! 

如果我想多一個層下來,我得到錯誤

irb 
require "watir-webdriver" 
browser = Watir::Browser.new :ff 
browser.goto "mangareader.net" 
browser.frame(:id, "adbottomleft").frame(:id, "adleft").html -> Don't work. 

元素屬於不同的幀比當前一個 - 切換到它包含框架使用它。 我應該在第二個代碼中更改哪些內容才能讀取下一個iframe?

我一直在尋找天。開始與硒然後htmunit與C#然後嘗試與Python的機械化,但無法實現想要的結果。

我一直在跳。我終於認爲我可以用WATIR實現我想要的東西。 我需要一些幫助才能完成。有小費嗎?

+1

我建議您從少量概念驗證腳本開始,以便熟悉WATIR。 browser.html吐出頁面源代碼html。它正在做它設計要做的事情。 Watir/Watir-Webdriver絕對可以在幾行代碼中完成你所要求的內容,但是你首先需要對庫的基本理解。 – 2012-02-29 21:34:15

回答

1

由腳本創建的幀的ID是「adbleft」而不是「adleft」這可能是你的問題

browser.frame(:id => "adbottomleft").frame(:id => "adbleft").html 

如果最後一幀的ID也不是一成不變的,你可能必須選擇它按索引

browser.frame(:id => "adbottomleft").frame(:id => "adbleft").frame(:index => 0) 
+0

是的,這工作!並感謝索引提示。這會幫助我! – user1237898 2012-02-29 22:57:16