LoginSignup
0
0

More than 1 year has passed since last update.

[初心者向け]rubyでスクレイピングを行い、その結果でメールを送信する

Last updated at Posted at 2022-06-11

例題

amazonでps5が売り出されていたらメールを送信する

まず、初めに

htmlから、この値を取り出す

リンク

スクリーンショット 2022-06-11 20.59.43.png

環境

macos
ruby 3.0.2

準備

必要ならインストールしてください

gem install open-uri
gem install nokogiri
gem install mail

1 まず、htmlを取得

amazon.rb
require 'open-uri'

html = URI.open('https://www.amazon.co.jp/PS5-CFI-1100A01-%E8%BB%BD%E9%87%8F%E7%89%88-%E3%83%87%E3%82%A3%E3%82%B9%E3%82%AF%E3%83%89%E3%83%A9%E3%82%A4%E3%83%96%E3%82%BF%E3%82%A4%E3%83%97-%E3%83%A1%E3%83%BC%E3%82%AB%E3%83%BC%E4%BF%9D%E8%A8%BC1%E5%B9%B4%E3%81%82%E3%82%8A/dp/B0B18BG131/ref=sr_1_1?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&crid=1VTPROMW1C2U9&keywords=ps5&qid=1654946758&sprefix=ps%2Caps%2C256&sr=8-1').read
puts html

2 出力結果でhtmlファイルを作成

~/lesson_ruby % rb amazon.rb > result.html

3 その結果から、好きな値を取得

nokogiri.rb
require 'nokogiri'

html = open('result.html').read
doc = Nokogiri::HTML.parse(html)
pp doc.xpath('//*[@id="availability"]/span[1]').inner_text.strip

4 結果

~/lesson_ruby % rb nokogiri.rb
"現在在庫切れです。"

if条件式を足す

nokogiri.rb
require 'nokogiri'

html = open('result.html').read
doc = Nokogiri::HTML.parse(html)
result_text = doc.xpath('//*[@id="availability"]/span[1]').inner_text.strip
puts result_text
if result_text != "現在在庫切れです。"
  puts "現在、売り出されてるようです"
else
  puts "まだ売り切れです"
end

結果

~/lesson_ruby % rb nokogiri.rb
現在在庫切れです。
まだ売り切れです

メール送信処理

if条件でtrueだったら、mailを送る、
とやれば、自分宛にメールが送られる。

その処理を、サーバーで定期実行すれば、
売り出された瞬間買えるはず。

ローカルmacからは送れないので、サーバーから実行してください

nokogiri.rb
require 'nokogiri'
require 'mail'

html = open('result.html').read
doc = Nokogiri::HTML.parse(html)
result_text = doc.xpath('//*[@id="availability"]/span[1]').inner_text.strip
puts result_text
if result_text != "現在在庫切れです。"
  Mail.deliver do
    from     'yourgmail@gmail.com'
    to       'yourgmail@gmail.com'
    subject  '販売されたよ'
    body     "販売されたよ"
    content_type 'text/plain; charset=UTF-8'
  end
  p "販売されたよ"
else
  Mail.deliver do
    from     'yourgmail@gmail.com'
    to       'yourgmail@gmail.com'
    subject  '現在在庫切れです。'
    body     "現在在庫切れです。"
    content_type 'text/plain; charset=UTF-8'
  end
  p "現在在庫切れです"
end

結果

迷惑メールボックスに入ってると思います
スクリーンショット 2022-06-11 21.25.46.png

参考文献

stripで空白を除去
https://docs.ruby-lang.org/ja/latest/method/String/i/strip.html

nokogiriでxpathで取得
https://qiita.com/momokahori/items/851e4c36c5b5c4c2ef98

nokogiri基礎(スクレイピング)
https://zenn.dev/arao99/articles/a68d8039f85fa8

open-uriで取得したhtmlを元に、スクレイピングする方法
https://qiita.com/momokahori/items/851e4c36c5b5c4c2ef98

文字コードやhtmlを有効にするかどうかのオプション
https://shepherdmaster.hateblo.jp/entry/2016/07/01/000000

改良するには

LINEAPIを利用して、LINEで通知を受け取った方が楽だと思う。
またはslack連携。

調べたらたくさん情報出てくるので、興味がある方はトライしてください

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0