通过JavaScript抓取Reddit数据 [英] grabbing Reddit data via javascript
问题描述
我碰到了这篇文章,这几乎是我想要做的:
I came across this post, which is pretty much what I'm looking to do:
我已经修改了提供的jsfiddle( NSFW http://jsfiddle .net/DHKtW/170/)显示为:
I have modified the jsfiddle that was provided (NSFW http://jsfiddle.net/DHKtW/170/) to read:
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) {
$.each(data.data.children, function(i,item){
console.log($('.score.likes').html(item));
});
});
我的目标是在给定的页面上收集全部支持.当您运行此命令并查看控制台时,将返回对象,而不是实际的数目.我以为只在选择器上调用html会返回票数,但显然是错误的.有更好的方法吗?
My goal is to gather the total amount of upvotes on a given page. When you run this and look at the console, objects are being returned but not the actual number. I thought just calling html on the selector would return the number of votes but apparently am wrong. Any better way of doing this?
推荐答案
您需要console.log(item)
才能查看返回的数据.使用它,我们可以看到item.data.score
返回帖子的分数.
You need to console.log(item)
to see the returned data. Using that we can then see that item.data.score
returns the score of a post.
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function (data) {
$.each(data.data.children, function (i, item) {
console.log(item.data);
$('<div/>', {
text: 'Post ' + item.data.permalink + ' has a score of ' + item.data.score
}).appendTo('#images');
});
});
http://jsfiddle.net/DHKtW/353/
看到没有数字的对象的原因是因为您调用了console.log($('selector'))
并且返回了jQuery对象而不是请求中的json.
The reason you saw objects without number is because you called console.log($('selector'))
and that returns the jQuery object not the json from the request.
这篇关于通过JavaScript抓取Reddit数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!