2013-02-26 12 views
6

Chcę pobrać wszystkie komentarze do CNN, którego system komentarzy to Disqus. Jako przykład: http://edition.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html?hpt=hp_c1Jak uzyskać wszystkie komentarze od Disqus?

System komentowania wymaga, abyśmy kliknęli "załaduj więcej", abyśmy mogli zobaczyć więcej komentarzy. Próbowałem użyć php do parsowania html, ale nie było możliwe załadowanie wszystkich komentarzy od czasu użycia javascript. Więc zastanawiam się, czy ktoś ma wygodniejszy sposób, aby odzyskać wszystkie komentarze z określonego URL-a cnn.

Czy ktoś wykonał ją z powodzeniem? Z góry dziękuję

Odpowiedz

6

Interfejs Disqus API zawiera metodę paginacji za pomocą kursorów, które są zwracane w odpowiedzi JSON. Zobacz tutaj aby uzyskać informacje na temat kursorów: http://disqus.com/api/docs/cursors/

Skoro wspomniałeś PHP, coś jak to powinno Ci zacząć:

<?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

Thanks a lot !!! Ale czego potrzebujemy dokładnie dla $ wątku (URL wątku) i kursora $? BTW, czy możemy mieć tylko 100 komentarzy co najwyżej? –

+0

Adres URL wątku jest po prostu adresem URL strony, na której znajdują się komentarze. W tym przypadku jest to http://www.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html - Wartość kursora jest pobierana z odpowiedzi interfejsu API, a reprezentuje następny zestaw 100 komentarzy. Skrypt zostanie uruchomiony, dopóki nie będzie już żadnych komentarzy. –

+0

Ustawiłem skrót $ jako "cnn" (var disqus_shortname = 'cnn';) i $ thread jako "link: 'i pozostawił kursor pusty, ale się okazało "Błąd parsowania json". Czy tęsknie za czymś? –

3

Wystarczy dodatek: aby uzyskać URL Disqus komentarze na dowolnej stronie, że to znaleźć, uruchomić ten kod JavaScript w konsoli przeglądarki internetowej:

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; 
}(); 

Ponieważ zmienna jest teraz w „wizyty”

console.log(visit); 

Pomogłem ci wydobyć wszystkie dane do formatu json UTF-8, zapisałem go do .txt i można go znaleźć pod tym link. Format json zawiera niektóre nazwy zmiennych, ale potrzebna jest zmienna "data", która jest tablicą JavaScript.

Powtórz powyższe czynności, a następnie podziel je na "x == x". "X == x" zostało zrobione, aby upewnić się, że identyfikator użytkownika tych, którzy dokonali komentarzy, również został przechwycony. W sytuacji, gdy nie ma identyfikatora użytkownika w formacie liczbowym, ale nazwa, oznacza to, że konto nie jest już aktywne.

Aby użyć userid, to kwestia https://disqus.com/users/106222183 gdzie jest userid

-1

bez 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> 
Powiązane problemy