Last active
August 29, 2015 14:11
-
-
Save dylandy/50bc8ce9a28c0ec082a4 to your computer and use it in GitHub Desktop.
Dcard 發文男女比
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'json' | |
require 'open-uri' | |
module Counter | |
def self.go! | |
count() | |
end | |
def self.count | |
gender = [] | |
# Dcard 97 以後的 api 不回應 | |
(1..97).each do |i| | |
begin | |
tmp = JSON.load(open("http://www.dcard.tw/api/forum/all/#{i}/")) | |
rescue | |
# 中間出現 error 時先讓 server 休息一下 | |
puts "error in #{i} sleeping..." | |
puts $! | |
puts $@ | |
sleep(30) | |
tmp = JSON.load(open("http://www.dcard.tw/api/forum/all/#{i}/")) | |
end | |
tmp.each do |j| | |
gender << j["member"]["gender"] | |
end | |
puts "fetched #{i}" | |
end | |
#約等於 1.5 | |
puts gender.count("F")/ gender.count("M").to_f | |
end | |
end | |
Counter.go! |
JS ajax 版本:
var male = 0;
var female = 0;
for( var i = 1 ; i < 98 ; i++ ){
$.ajax({
type: "GET",
url: "http://www.dcard.tw/api/forum/all/" + i + "/",
dataType: "json",
success: function(json){
$(json).each(function(){
if(this.member.gender=="M"){male+=1}else{female+=1}
})
},
error: function(){
console.log("error");
}
});
}
//印出發文者男女比
console.log((female / male)*100)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JS 無 ajax 版本: