我一直在使用咖啡脚本中的 node.js 来从 reddit.com 的 json 界面中提取一些故事,但遇到了一些障碍。
我想从中解析json http://www.reddit.com/r/programming/.json
,然后附加一个带有参数的查询语句count
并after
重新解析。根据传递给的参数重复get_stories()
当我运行下面的代码时,file.js > test.txt
我得到了意想不到的结果。(见下文)看起来querystring.count
正在更新,但它们都与最后一遍中的 url 相匹配。不知道为什么我没有看到 count=0,25,50,75,125。此外,querystring.after
网址上不存在。到底是怎么回事?
代码:
# Requires
request = require 'request'
qs = require 'querystring'
mongojs = require 'mongojs'
# Connect to db
db = mongojs 'mongodb://localhost/feedtraining', ['subreddit_stories']
get_stories = (subreddit, {per_page, pages}, storyCallback) ->
current_page = 0
querystring = {}
while true
querystring.count = current_page * per_page
request_uri = "http://www.reddit.com/r/#{subreddit}/.json?#{qs.stringify querystring}"
request
uri: request_uri,
json: true,
(error, response, body) ->
if !error and response.statusCode == 200
for item in body.data.children
if item.data.selftext_html is null
storyCallback request_uri, current_page, item.data
querystring.after = body.data.children[body.data.children.length-1].id
else
console.log error
return
if current_page == pages then break else current_page++
return
get_stories 'programming', {per_page: 25, pages: 5}, (request_uri, page, story) ->
db.subreddit_stories.insert(story)
console.log request_uri
输出:
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125
http://www.reddit.com/r/programming/.json?count=125