做一个rails的脚本,使用httparty 进行接口内容的抓取

首先,在项目文件中,Gemfile 增加

gem install httparty

然后进行执行命令,然后重启服务

bundle install

明确,要实现的是这样的内容

GET https://ethgasstation.info/api/ethgasAPI.json

抓取内容,打印出 fastest 的值

提示:使用 httparty, JSON.parse

创建一个rb文件,类似这样的

ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'rails'
require 'rubygems'
require 'json'

puts 'lueluelue'

在命令行执行,

ruby 文件名

就可以看到打印出来的“lueluelue”

同样的,也可执行我们需要的内容,文件上半部分的内容是固定不变,如果有报错就根据提示注释一下上边的内容,比如注释一下第二行,一般是不必的。

ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production'
#require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'rails'
require 'rubygems'
require 'json'

require 'httparty'


temp = HTTParty.get "https://ethgasstation.info/api/ethgasAPI.json"

puts temp.body

temp2 = JSON.parse(temp.body)

puts temp2["fastest"]
puts temp2["safeLow"]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值