javascript - 通过 PhantomJS 获取 Google Ads 内容

标签 javascript phantomjs web-crawler

我想在网页上获取 Google Ads 内容。 我正在使用 phantomJS,当我渲染图像时,我可以看到谷歌广告 block 。 但是当我获取源代码(html)时,它只是 javascript 代码。

我的简单代码。

var fs = require('fs');
var page = require('webpage').create();
var url = "http://www.thegeekstuff.com/2016/04/oracle-undo-tablespace/";

page.open(url);

page.onLoadFinished = function()
{

    fs.write("source.htm", page.content, 'w');
    page.render('render.png');
    phantom.exit();

};

渲染图像包含 Google 广告内容。

enter image description here

但是 HTML 源代码:

<p>In the above example, we have two UNDO tablespace listed. But only one of them can be active and used by the system. The other one is currently not used.</p>
<p>So, the best way to view the current valid UNDO tablespace is by using “show parameter” as shown below.</p>
<center>
  <div style="margin-left:2px; margin-top:10px; margin-bottom:10px; ">

    <!-- AD BLOCK -->

 <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- TGS Inside Content -->
<ins class="adsbygoogle" style="display: inline-block; width: 300px; height: 250px;" data-ad-client="ca-pub-8090601437064582" data-ad-slot="8643685131" data-adsbygoogle-status="done"><ins id="aswift_1_expand" style="display:inline-table;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><ins id="aswift_1_anchor" style="display:block;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><iframe width="300" height="250" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_1" name="aswift_1" style="left:0;position:absolute;top:0;"></iframe></ins></ins></ins>
 <script>
 (adsbygoogle = window.adsbygoogle || []).push({});
</script>

    <!-- END AD BLOCK -->

最佳答案

查看页面输出,似乎有一个名为“aswift_1”的 iframe。这又包含一个名为“google_ads_frame2”的 iframe。我不太熟悉Google广告的结构,但乍一看,这些看起来是用来输出广告内容的。

如果您想使用 phantomjs 获取此 iframe 内容,则可以使用如下代码:

var fs = require('fs');
var page = require('webpage').create();
var url = "http://www.thegeekstuff.com/2016/04/oracle-undo-tablespace/";

page.open(url,  function(status) {
    if ('success' !== status) {
        console.log("Error");
    } else {
        page.switchToChildFrame('aswift_1');
        page.switchToChildFrame('google_ads_frame2');
        fs.write("test-google-source.htm", page.frameContent, 'w');
        page.render('test-google-render.png');
    }
    phantom.exit();
});

关于javascript - 通过 PhantomJS 获取 Google Ads 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36887361/

相关文章:

javascript - phantomJS爬取多页面获取错序执行

java - 使用 ExecutorService 并行处理作业

javascript - jQuery 性能 : Chain appends or append multiple arguments?

javascript拼接方法与返回混淆

java - 如何在另一个 html 标签内选择带有 html 源的 web 元素 selenium web 驱动程序

selenium - 水貂 + PhantomJS : How do I set the user agent?

javascript - 如何在 python selenium 中禁用 PhantomJS 的屏幕截图和 javascript?

javascript - PhantomJS 使用太多线程

c# - 如何从另一个用 AngularJS 构建的网站上抓取数据?

javascript - PhantomJS 无法打开未知扩展名的本地文件