使用disqus从网站检索评论

qwe*_*tyl 5 disqus web-scraping

我想写一个抓取脚本来从cnn文章中检索评论.例如,这篇文章:http://www.cnn.com/2012/01/19/politics/gop-debate/index.html?hpt=hp_t1

我意识到cnn使用disqus进行评论讨论.由于评论加载不是基于网页的(即上一页,下一页)并且是动态的(即需要点击"下一个加载25"),我不知道如何检索本文的所有5000多条评论.

有什么想法或建议吗?

非常感谢!

med*_*116 5

我需要通过ajax抓取一个有disqus评论的页面来获得评论.因为它们没有在服务器上呈现,所以我不得不调用disqus api.在源代码中,您将需要标识符代码:

var identifier = "456643" // take note of this from the page source
// this is the ident url query param in the following js request
Run Code Online (Sandbox Code Playgroud)

另外,查看js源代码以获取页面公钥和论坛名称.将它们放在适当的URL中.

我使用javascript nodejs来测试它,即:

var request = require("request");

var publicKey  = "pILMw27bsbJsdfsdQDh9Eh0MzAgFL6xx0hYdsdsdfaIfBHRvLGqFFQ09st";

var disqusUri = "https://disqus.com/api/3.0/threads/listPosts.json?&api_key=" + publicKey + "&thread:ident=456643&forum=nameOfForumFromSource";

request(disqusUri, function(res,status,err){
    console.log(res.body);

    if(err){
        console.log("ERR: " + err);
    }
});
Run Code Online (Sandbox Code Playgroud)


Boa*_*oaz 3

抓取(除了获取页面之外)的选项可能不太健壮(取决于您的需求),但将为您遇到的问题提供解决方案,即在成熟的网络浏览器周围使用某种包装器,并且从字面上对使用模式进行编码并提取相关数据。由于您没有提到您知道哪种编程语言,我将给出 3 个示例:1) Watir - ruby​​,2) Watin - 通过 .net 的 IE 和 Firefox,3) Selenium - 通过 C#/Java/Perl/PHP/ 的 IE红宝石/Python

我将提供一个使用 Watin 和 C# 的小例子:

IE browser = new IE();
browser.GoTo(YOUR CNN URL);
List visibleComments = Browser.List(Find.ById("dsq-comments"));
//do your scraping thing
Link moreComments = Browser.Link(Find.ByClass("dsq-paginate-append-text");
moreComments.click();
//wait util ajax ended by searching for some indicator
Browser.WaitUntilContainsText(SOME TEXT);
//do your scraping thing
Run Code Online (Sandbox Code Playgroud)

注意:我不熟悉 disqus,但通过循环链接并单击我发布的代码部分来强制显示所有注释可能是一个更好的选择,直到所有注释都可见并刮掉列表元素 dsq-评论