總結(jié)
昨天練習(xí)了兩次WEP API仅乓,花了八個(gè)小時(shí)咨演,在后面的兩個(gè)小時(shí)做作業(yè)攻走,還是沒做出來谓媒。今天花了兩個(gè)小時(shí)做了兩個(gè)出來:一個(gè)是抓云幣的數(shù)據(jù)淆院,參考同學(xué):https://forum.qzy.camp/t/api/1687;另一個(gè)是抓足球賽事的數(shù)據(jù)句惯。
總結(jié)一下:
先問問題:我是怎么想得土辩?怎么做的支救?結(jié)果怎樣?花了多長(zhǎng)時(shí)間拷淘?我要總結(jié)什么各墨?有什么要改?有什么優(yōu)點(diǎn)和好的做法要繼續(xù)的保持启涯?套路是什么贬堵?
解答:
- 我是如此想: 對(duì)不熟的東西,要先打個(gè)照面结洼;先做一遍黎做,看出來的效果,然后把不懂的抄寫松忍。
- 如此做:我還是按照先做一遍蒸殿,然后手打一遍,沒有復(fù)盤挽铁,然后對(duì)不懂的手抄了一兩遍。
- 昨天結(jié)果是很懵逼敞掘,感覺很失敗叽掘,沒有成就感。今天做完了作業(yè)也感覺沒什么玖雁,也不是很難更扁。
- 好的做法要保持:
- 連續(xù)專注的學(xué)習(xí)
- 不怕困難
- 要改進(jìn)的地方:
- 第一遍復(fù)制黏貼,不需要手打赫冬;第二遍邊手打邊思考浓镜,看輸出什么,什么代碼有怎樣的結(jié)果劲厌;然后嘗試做一個(gè)類似的東西膛薛、項(xiàng)目出來(這個(gè)很重要);最后總結(jié)套路出來补鼻。
- 寫總結(jié)哄啄,還有“進(jìn)步本”;總結(jié)套路风范,記錄所學(xué)知識(shí)及要點(diǎn)咨跌,及時(shí)復(fù)習(xí)。
API套路
一硼婿、首先拿數(shù)據(jù)锌半,在終端測(cè)試
用 rest-client 抓下來觀察看看,拿我用的聚合數(shù)據(jù)里面足球積分榜的數(shù)據(jù)來做:
接下來實(shí)驗(yàn) Ruby 客戶端寇漫,用 Ruby 程序來抓取上述的資料刊殉。
進(jìn)入 irb:
gem install rest-client
require 'rest-client'
require 'json'
response = RestClient.get "http://v.juhe.cn/football/scorerank.php", :params => { :key => "5a24681a17011d0d74229ed30f13fd77", :league_id => "2" }
data = JSON.parse(response.body)
我一直糾結(jié)不知道怎么把這個(gè)需要參數(shù)的數(shù)據(jù)提出來殉摔,后來看到這個(gè)key的輸入方式,想到加入這個(gè)
league_id
的方式冗澈,放進(jìn)去可以奏效钦勘。
成功的話會(huì)顯示如下:
那這個(gè)數(shù)據(jù)可以用咯。
二亚亲、建立專案
- 建立Model
rails new api_(自己取名)
cd api_(上面的名字)
git init
加入gem gem 'rest-client
,然后bundle install
執(zhí)行rails g model scoreranks
這個(gè)model名字沒改好彻采,感覺改teams更好。
編輯這個(gè)檔案db/migrate/2017xxxxxxxx_create_scroeranks.rb
class CreateScoreranks < ActiveRecord::Migration[5.0]
def change
create_table :scoreranks do |t|
t.integer :juhe_id
t.integer :league_id
t.integer :team_group
t.integer :team_id
t.string :name_zh
t.string :known_name_zh
t.integer :played
t.integer :rank_index
t.integer :win
t.integer :draw
t.integer :lost
t.integer :hits
t.integer :miss
t.integer :difference
t.integer :score
t.string :avg_goal_hit
t.string :avg_goal_lost
t.string :avg_goal_win
t.string :avg_score
t.integer :season_id
t.timestamps
end
end
end
然后rake db:migrate
- 抓取數(shù)據(jù)儲(chǔ)存
新增lib/tasks/dev.rake
namespace :dev do
task :fetch_scorerank => :environment do
puts "Fetch scorerank data..."
response = RestClient.get "http://v.juhe.cn/football/scorerank.php", :params => { :key => "5a24681a17011d0d74229ed30f13fd77", :league_id => "2", :season_id => "2016" }
data = JSON.parse(response.body)
data["result"].each do |c|
existing_scorerank = Scorerank.find_by_juhe_id( c["id"] )
if existing_scorerank.nil?
Scorerank.create!( :juhe_id => c["id"], :season_id => c["season_id"],
:league_id => c["league_id"],:team_group => c["team_group"], :team_id => c["team_id"],
:name_zh => c["name_zh"], :known_name_zh => c["known_name_zh"], :played => c["played"],
:rank_index => c["rank_index"], :win => c["win"], :draw => c["draw"], :lost => c["lost"],
:hits => c["hits"], :miss => c["miss"], :difference => c["difference"], :score => c["score"],
:avg_goal_hit => c["avg_goal_hit"], :avg_goal_lost => c["avg_goal_lost"],
:avg_goal_win => c["avg_goal_win"], :avg_score => c["avg_score"] )
end
end
puts "Total: #{Scorerank.count} Scorerank"
end
end
執(zhí)行 bundle exec rake dev:fetch_scorerank
就會(huì)把數(shù)據(jù)存入了捌归。
上面這塊我糾結(jié)了最長(zhǎng)時(shí)間肛响,老是bundle都顯示錯(cuò)誤,首先create!后面的括號(hào)要緊跟惜索,別有空格特笋,另外格式要對(duì),別少了字母c巾兆。
后面就可以按照教程來做了猎物。