作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想获取评论系统是 Disqus 的 CNN 上的所有评论。
例如,http://edition.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html?hpt=hp_c1
评论系统需要我们点击“加载更多”才能看到更多评论。
我曾尝试使用 php 解析 html,但由于使用了 javascript,因此无法加载所有评论。
所以我想知道是否有人有更方便的方法来从特定的 cnn url 中检索所有评论。
有人成功过吗?
提前致谢
最佳答案
Disqus API 包含使用在 JSON 响应中返回的游标的分页方法。有关游标的信息,请参见此处:http://disqus.com/api/docs/cursors/
既然你提到了 PHP,这样的事情应该让你开始:
<?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);
}*/
}
}
?>
关于disqus - 如何从 Disqus 获取所有评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080258/
我是一名优秀的程序员,十分优秀!