2013-02-26 30 views
6

Voglio recuperare tutti i commenti su CNN il cui sistema di commenti è Disqus. Ad esempio, http://edition.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html?hpt=hp_c1Come ottenere tutti i commenti da Disqus?

Il sistema di commenti richiede di fare clic su "carica di più" in modo da poter visualizzare più commenti. Ho provato ad usare php per analizzare l'html ma non è stato in grado di caricare tutti i commenti da quando è stato utilizzato javascript. Quindi mi chiedo se qualcuno ha un modo più conveniente per recuperare tutti i commenti da un URL cnn specifico.

Qualcuno l'ha fatto con successo? Grazie in anticipo

risposta

6

L'API Disqus contiene un metodo di impaginazione che utilizza cursori restituiti nella risposta JSON. Vedi qui per informazioni sui cursori: http://disqus.com/api/docs/cursors/

Dal momento che lei ha citato PHP, qualcosa di simile dovrebbe iniziare:

<?php 
$apikey = '<your key here>'; // get keys at http://disqus.com/api/ — can be public or secret for this endpoint 
$shortname = '<the disqus forum shortname>'; // defined in the var disqus_shortname = '...'; 
$thread = 'link:<URL of thread>'; // IMPORTANT the URL that you're viewing isn't necessarily the one stored with the thread of comments 
//$thread = 'ident:<identifier of thread>'; Use this if 'link:' has no results. Defined in 'var disqus_identifier = '...'; 
$limit = '100'; // max is 100 for this endpoint. 25 is default 

$endpoint = 'https://disqus.com/api/3.0/threads/listPosts.json?api_key='.$apikey.'&forum='.$shortname.'&limit='.$limit.'&cursor='.$cursor; 

$j=0; 
listcomments($endpoint,$cursor,$j); 

function listcomments($endpoint,$cursor,$j) { 

    // Standard CURL 
    $session = curl_init($endpoint.$cursor); 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); // instead of just returning true on success, return the result on success 
    $data = curl_exec($session); 
    curl_close($session); 

    // Decode JSON data 
    $results = json_decode($data); 
    if ($results === NULL) die('Error parsing json'); 

    // Comment response 
    $comments = $results->response; 

    // Cursor for pagination 
    $cursor = $results->cursor; 

    $i=0; 
    foreach ($comments as $comment) { 
     $name = $comment->author->name; 
     $comment = $comment->message; 
     $created = $comment->createdAt; 
     // Get more data... 

     echo "<p>".$name." wrote:<br/>"; 
     echo $comment."<br/>"; 
     echo $created."</p>"; 
     $i++; 
    } 

    // cursor through until today 
    if ($i == 100) { 
     $cursor = $cursor->next; 
     $i = 0; 
     listcomments($endpoint,$cursor); 
     /* uncomment to only run $j number of iterations 
     $j++; 
     if ($j < 10) { 
      listcomments($endpoint,$cursor,$j); 
     }*/ 
    } 
} 

?> 
+0

Grazie mille !!! Ma di cosa abbiamo bisogno esattamente per il $ thread (URL del thread) e $ cursor? A proposito, possiamo avere solo 100 commenti al massimo ?? –

+0

L'URL del thread è solo l'URL della pagina in cui si trovano i commenti. In questo caso è http://www.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html - Il valore del cursore viene estratto dalla risposta dell'API e rappresenta la prossima serie di 100 commenti. Lo script andrà fino a quando non ci saranno più commenti. –

+0

Ho impostato $ shortname come 'cnn' (var disqus_shortname = 'cnn';) e $ thread come 'link: 'e mantenuto $ cursor vuoto, ma risulta "Error parsing json". Mi manca qualcosa ?? –

3

solo un'aggiunta: per ottenere l'url di commenti Disqus in qualsiasi pagina che viene trovata, eseguire questo codice JavaScript nella console web browser:

var visit = function() { 
var url = document.querySelector('div#disqus_thread iframe').src; 

String.prototype.startsWith = function (check) { 
    return(this.indexOf(check) == 0); 
}; 

if (!url.startsWith('https://')) return url.slice(0, 4) + "s" + url.slice(4); 

return url; 
}(); 

Poiché la variabile è ora in 'visita'

console.log(visit); 

Ti ho aiutato a estrarre tutti i dati in un formato json UTF-8, salvato in .txt e può essere trovato in questo link. Il formato json contiene alcuni nomi di variabili, ma quello di cui hai bisogno è la variabile "data", che è un array JavaScript.

Iterate attraverso ciascuna di esse e quindi dividetele in 'x == x'. Il 'x == x' è stato fatto per assicurarsi che l'userid di coloro che hanno fatto i commenti anche dove catturato. In una situazione in cui non esiste un userid in formato numerico ma un nome, significa che l'account non è più attivo.

Per utilizzare il userid, è una questione di https://disqus.com/users/106222183 dove il 106.222.183 è l'userid

-1

senza api:

#disqus_thread { 
    position: relative; 
    height: 300px; 
    background-color: #fff; 
    overflow: hidden; 
} 
#disqus_thread:after { 
    content: ""; 
    display: block; 
    height: 10px; 
    width: 100%; 
    position: absolute; 
    bottom: 0; 
    background: white; 
} 
#disqus_thread.loaded { 
    height: auto; 
} 
#disqus_thread.loaded:after{ 
    height:55px; 
} 
#disqus-load { 
    text-align: center; 
    color: #fff; 
    padding: 11px 14px; 
    font-size: 13px; 
    font-weight: 500; 
    display: block; 
    text-align: center; 
    border: none; 
    background: rgba(29,47,58,.6); 
    line-height: 1.1; 
    border-radius: 3px; 
    font-weight: 500; 
    transition: background .2s; 
    text-shadow: none; 
    cursor:pointer; 
} 

<div class="disqus-comments"> 
    <div id='disqus_thread'></div> 
    <div id='disqus-load'>Load comments</div> 
</div> 

<script type="text/javascript"> 


$(document).ready(function() { 
    var disqus_shortname = 'testare-123'; 

    (function() { 
     var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; 
     dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; 
     (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); 
    })(); 
     $('#disqus-load').on('click', function(){ 

     $.ajax({ 
      type: "GET", 
      url: "http://" + disqus_shortname + ".disqus.com/embed.js", 
      dataType: "script", 
      cache: true 
     }); 

     $(this).fadeOut(); 
     $('#disqus_thread').addClass('loaded'); 
    }); 
}); 
    /* * * CONFIGURATION VARIABLES * * */ 
    // var disqus_shortname = 'testare-123'; 

    // /* * * DON'T EDIT BELOW THIS LINE * * */ 
    // (function() { 
    // var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; 
    // dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; 
    // (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); 
    // })(); 
</script> 
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript> 
Problemi correlati