2017-08-24 53 views
0

我想登錄並從這個頁面刮一個值:https://adf.ly,特別是我的鏈接有多少意見。我已經實現了登錄並進入到我想要的值出現的頁面,但問題是這個值沒有加載頁面,稍後使用javascript函數加載。我用的代碼是以下之一:。「一直有問題,而獲取的統計信息請刷新頁面,然後再試一次」刮一個網頁,並得到一個值改變一個javascript功能

<?php 
    function getViewsAdfly(){ 
     $cookiePath = './cookieAnuncios.txt'; 
     $url = "https://login.adf.ly/login"; 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0'); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language: es-es,en")); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
     curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath); 
     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $result = curl_exec($ch); 

     $token = urlencode(getMatchToken($result)); 
     $email = urlencode("my email"); 
     $password = urlencode("my password"); 
     $parametrosPost = "token=" . $token . "&bmlUrl=&bmlType=&bmlDomain=&bmlFolder=&dest=&response=&challenge=&email=" . $email . "&password=" . $password; 

     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $parametrosPost); 
     curl_exec($ch); 

     $urlViews = "https://adf. ly/publisher"; 

     curl_setopt($ch, CURLOPT_URL, $urlViews); 
     curl_setopt($ch, CURLOPT_POST, 0); 
     $resultViews = curl_exec($ch); 
     curl_close($ch); 

     return getMatchViewsAdfly($resultViews); 
    } 

    function getMatchToken($result){ 
     preg_match_all("(<input type=\"hidden\" name=\"token\" id=\"token\" value=\"(.*)\" />)siU", $result, $matchesToken, PREG_PATTERN_ORDER); 
     return $matchesToken[1][0]; 
    } 

    function getMatchViewsAdfly($result){ 
     preg_match_all("(<h4 id=\"summary-views\">(.*)</h4>)siU", $result, $matchesViews, PREG_PATTERN_ORDER); 
     return $matchesViews[1][0]; 
    } 
?> 

如果我使用此代碼,頁面顯示一條消息說而其中包含我想要刮除的數據的<h4 id=\"summary-views\"></h4>顯示「加載...」。我認爲在cointainer上收取實際數字的JavaScript方法運行不正確。我是否必須添加任何缺少curl setopt以便javascript運行?在容器加載後我怎樣才能獲得真正的數據呢?

編輯:

的JavaScript節省s[0]數據。任何方式我可以得到存儲在那裏?

$(document).ready(function() { 
       $('#edit_link').dialog({ 
        autoOpen: false, 
        height: AdFly.awesomeBrowser() ? 420 : 450, 
        width: AdFly.awesomeBrowser() ? 550 : 580, 
        modal: true, 
        closeOnEscape: false, 
        resizable: false 
       }); 

       $('#edit_advert_type').chosen(); 

       $('#charts-tabs, #links-tabs').easytabs({animate: false, updateHash: false, tabs: "nav > ul > li"}); 

       $('#charts-tabs') 
         .bind('easytabs:after', function (event, $clicked, $targetPanel, settings) { 
          PublisherIndex.updateCountries(); 
          $('ul.earnings').addClass('displayNone'); 
          if($targetPanel.selector == '#charts-tab-4'){ 
           $('ul.earnings.popads').removeClass('displayNone'); 
          }else{ 
           $('ul.earnings.simplelinks').removeClass('displayNone'); 
          } 
         }); 

       $('#report_date').chosen(); 
       $('#report_date').change(function() { 
        $('.timePicker').removeClass('current'); 
        $(this).parent().addClass('current'); 
        PublisherIndex.renderStats(); 
        PublisherIndex.updateLinks(); 
       }); 

       $('.timePicker').click(function() { 
          if ($(this).parent().attr('class') == 'all_time') { 
         alert("Sorry all time stats are currently disabled."); 
         return; 
        } 

        $('.timePicker').removeClass('current'); 
        $('#report_date').parent().removeClass('current'); 
        $(this).addClass('current'); 
        PublisherIndex.renderStats(); 
        PublisherIndex.updateLinks(); 
        PublisherIndex.renderSummary(); 
        return false; 
       }); 

       $('#findlink').keypress(function(event) { 
        var keycode = (event.keyCode ? event.keyCode : event.which); 
        if (keycode == '13') { 
         PublisherIndex.updateLinks(); 
         $('#search_clear').show(); 
        } 
       }); 

       $('#links-tabs').bind('easytabs:after', function(event, $clicked, $targetPanel, settings) { 
        PublisherIndex.updateLinks(); 
       }); 

       PublisherIndex.getAnnouncements(); 
       //PublisherIndex.renderSummary(); 
       PublisherIndex.renderStats(); 
       PublisherIndex.renderLinks(); 

       $('.monthlyEarningsSummarySelector').click(function() { 
        $('.monthlyEarningsSummarySelector').removeClass('current'); 
        $(this).addClass('current'); 
        PublisherIndex.renderStats(); 
        PublisherIndex.renderSummary(); 
        return false; 
       }); 
      }); 

      $(document).ready(function() { 
       // Little hack for getting the URL Groups. 
       $('#edit_group').html($('section.shorten select[name="group"]').html()); 
       $('#edit_group').chosen(); 
      }); 

      var PublisherIndex = { 
       countriesDt: null, 
       countriesLength: null, 
       referralsDt: null, 
       earningsChart: null, 
       graphData: null, 
       popupsChart: null, 
       popupGraphData: null, 
       earningsSummary: null, 
       tokenCSRF: null, 
       linkId: null, 
       updateCountries: function() { 
        var oTable = $('#charts-tab-2').find('table').dataTable(); 
        var table_height = 419; 
        var num_rows = this.countriesLength; 
        var row_height = $('section.dashboard section.charts table tr').outerHeight(); 
        if(num_rows<10){ 
         num_rows = 10; 
        }else if(num_rows>20){ 
         num_rows=20; 
        } 
        var required_height = parseInt(table_height) + (parseInt(num_rows)-10)*parseInt(row_height); 
        $('section.dashboard section.charts nav.menu').css('height', parseInt(required_height)); 
        $('section.dashboard section.charts .data .panels').css('height', parseInt(required_height)+8); 
        var oSettings = oTable.fnSettings(); 
        oSettings._iDisplayLength = num_rows; 
        oTable.fnDraw(); 
       }, 
       getAnnouncements: function() { 
        ApiCaller.getAnnouncements(AdFly.USER_TYPE.PUBLISHER, $('#announcements'), false); 
       }, 
       renderSummary: function() { 
        $('ul.earnings:not(.totals)').css('visibility','visible'); 
        $t = $('.timePicker.current, #reportDateWrapper.current'); 
        var key = null; 
        var copy = null; 
        key = $("#report_date").val(); 
        var i = 1; 
        copy = $('.chzn-container-single .chzn-single span').eq(3).text(); 
        copy = copy.substring(0, copy.length - 5); 
        var views = 0; 
        var earnings = 0; 
        var referrals = 0; 
        var total = 0; 
        $.each(this.graph.data,function(i,day){ 
         views+=parseFloat(day.views); 
         earnings+=parseFloat(day.earnings); 
        }); 
       } 
      } 

在此先感謝您。

+1

你不能像使用curl那樣使用curl來執行javascript。 – Scuzzy

+0

但是,javascript代碼包含在web'curl_exec($ ch)'中。它不應該與頁面的其餘部分一起運行嗎? @Scuzzy – Luiscri

+0

不,因爲你正在下載的只是一個普通的原始html文檔。與瀏覽器相比,瀏覽器下載html文檔並通過其引擎運行,將其解析爲文檔對象模型並應用規則並運行其他腳本,而curl是該過程的一部分,而不是整個過程。 – Scuzzy

回答

1

Curl不執行JS。你有兩種選擇:

  1. 使用一些技術來執行JS。這在PHP中很難,你不應該嘗試它。 (如果你想這樣做,那麼你需要一個PHP引擎中的JS引擎,如V8)。嘗試使用,而不是PhantomJS。您可以創建一個運行JS代碼服務器端的Web服務(Node.js),然後通過API通過PHP獲取數據。
  2. 以與JS函數相同的方式獲取數據。只有兩種選擇:
    • 數據在頁面交付後加載。然後他們必須使用HTTP請求。找到這個請求並模仿它。
    • 數據在交付頁面的某處寫下,但尚未顯示。也許它存儲在函數調用的JS變量中。 (我的意思是已經構建服務器端和包括變量已經JS代碼)

祝你好運!

+0

這是第二個選項。Javascript代碼是這樣的: 'this.earningsSummary [key] = {views:views,earnings:earnings,referrals:referrals,total:total}; ......更多代碼....... var s = [this.earningsSummary [key] .views,this.earningsSummary [key] .earnings,this.earningsSummary [key] .referrals,this。 earningsSummary [鍵]。總]; $('#summary-views')。html(Globalize.format(s [0],'n0'));' 所以數據保存在s [0]上,然後通過jquery $(「#containerID 「)。html的(......)。有什麼方法可以將數據存儲在那裏? – Luiscri

+0

對不起,代碼在這裏沒有清楚的顯示。我用相同的代碼編輯了原文。 – Luiscri

+0

它必須設置在某個地方,打開頁面的代碼並用Ctrl + F搜索一個值。比方說,如果您有127個點擊的鏈接,請搜索它。它必須在代碼中的某處。你不能得到變量的值,因爲你沒有執行JS代碼。 –