2013-02-26 26 views
6

Tôi muốn tìm nạp tất cả các nhận xét trên CNN có hệ thống nhận xét là Disqus. Ví dụ: http://edition.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html?hpt=hp_c1Làm thế nào để có được tất cả các ý kiến ​​từ Disqus?

Hệ thống nhận xét yêu cầu chúng tôi nhấp vào "tải thêm" để chúng tôi có thể xem thêm nhận xét. Tôi đã thử sử dụng php để phân tích cú pháp html nhưng không thể tải tất cả các nhận xét vì javascript được sử dụng. Vì vậy, tôi tự hỏi nếu có ai có một cách thuận tiện hơn để lấy tất cả các ý kiến ​​từ một url cụ thể cnn.

Có ai đã thực hiện thành công không? Cảm ơn trước

Trả lời

6

API Disqus chứa phương pháp phân trang bằng cách sử dụng con trỏ được trả về trong phản hồi JSON. Xem ở đây để biết thông tin về con trỏ: http://disqus.com/api/docs/cursors/

Kể từ khi bạn đề cập đến PHP, một cái gì đó như thế này sẽ giúp bạn bắt đầu:

<?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 !!! Nhưng chúng ta cần chính xác cái gì cho chuỗi $ (URL của chuỗi) và $ cursor? BTW, chúng ta có thể chỉ có 100 bình luận nhiều nhất không ?? –

+0

URL của chuỗi chỉ là URL của trang mà các bình luận đang bật. Trong trường hợp này, http://www.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html - Giá trị con trỏ được lấy từ phản hồi API và đại diện cho bộ tiếp theo gồm 100 nhận xét. Tập lệnh sẽ xuất hiện cho đến khi không còn nhận xét nào nữa. –

+0

Tôi đã đặt $ shortname là 'cnn' (var disqus_shortname = 'cnn';) và $ thread as 'link: 'và giữ $ con trỏ trống, nhưng nó chỉ ra "Lỗi phân tích cú pháp json". Tôi có nhớ gì không? –

3

Chỉ cần một sự bổ sung: để có được url của bình luận Disqus trên bất kỳ trang nào mà nó tìm thấy, chạy mã JavaScript này trong trình duyệt web giao diện điều khiển:

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

Kể từ khi biến bây giờ là trong 'lần'

console.log(visit); 

Tôi đã giúp bạn khai thác tất cả dữ liệu thành định dạng json UTF-8, lưu nó vào .txt và nó có thể được tìm thấy tại số link này. Định dạng json chứa một số tên biến nhưng cái bạn cần là biến 'dữ liệu', là một mảng JavaScript.

Lặp lại từng bước và sau đó chia chúng thành 'x == x'. Các 'x == x' đã được thực hiện để đảm bảo rằng userid của những người thực hiện các ý kiến, nơi bị bắt quá. Trong trường hợp không có userid ở định dạng số nhưng có tên, điều đó có nghĩa là tài khoản không còn hoạt động nữa.

Để sử dụng userid, đó là một vấn đề của https://disqus.com/users/106222183 nơi 106.222.183 là userid

-1

mà không 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> 
Các vấn đề liên quan