LoginSignup
1
1

More than 3 years have passed since last update.

[Go To トラベル] じゃらんでクオカード付きプランを検索してみた

Last updated at Posted at 2020-07-16

dockerを使う

docker-compose.yml
version: "3.7"
services:
  chrome:
    image: selenium/standalone-chrome
    shm_size: 256m

  backend:
    build:
      context: ./backend
    working_dir: /app
    volumes:
      - ./backend:/app
    command: ash -c "tail -f README.md"

rails cできればいいだけなので、docker-compose upしてもbackendが終了しないようにcommandtail -f

あとはdocker exec -it backend_1 ash的なコマンドで中に入る。

selenium/standalone-chromeをrails側から呼ぶ

models/concerns/chromeable.rb
require "selenium-webdriver"
require "capybara"

module Chromeable
  extend ActiveSupport::Concern

  DOCKER_CHROME_SELENIUM_HOST_NAME = "chrome"
  Capybara.register_driver :chrome do |app|
    args = %w[
      --headless
      --disable-gpu
      --window-size=1280,800
      --blink-settings=imagesEnabled=false
    ]

    caps =
      Selenium::WebDriver::Remote::Capabilities.chrome(
        "goog:chromeOptions" => {
          args: args,
          prefs: {
            credentials_enable_service: false,
            profile: { password_manager_enabled: false }
          }
        }
      )

    Capybara::Selenium::Driver.new(
      app,
      browser: :remote,
      desired_capabilities: caps,
      url: "http://#{DOCKER_CHROME_SELENIUM_HOST_NAME}:4444/wd/hub"
    )
  end

  included {}

  module ClassMethod; end

  def create_session
    session = Capybara::Session.new(:chrome)
    if block_given?
      begin
        yield session
      ensure
        session&.driver&.quit
      end
    else
      session
    end
  end
end

適当にこういうdocker-composeのchromeサービスを呼べるようなconcernを作って、じゃらんを検索し、表示されるプラン名に/([\d,]+)円/があるものをピックアップして、データベースに保存。
これを最後のページまでやっていく。

models/Jalan.rb
class Jalan < ApplicationRecord
  class Crawler
    include Chromeable

    def ensure_session
      @session = create_session
      session.visit "https://www.jalan.net/uw/uwp2011/uww2011init.do?keyword=%83N%83I%83J%81%5B%83h&distCd=06&rootCd=7701&screenId=FWPCTOP&image1.x=11&image1.y=13"
      session.select "1", from: "adultNum"
      session.click_on "再検索"

      yield
    ensure
      session&.driver&.quit
    end

    ...
  end 
end

結果

Jalan.group(:value).count
=> {3000=>201, 4000=>15, 5000=>37, 6000=>3, 7000=>2, 7700=>2, 8000=>1, 10000=>3, 30000=>2}

ノイズも入ってるけど ...

見つけたすごいプラン

3万円のクオカードが付く5万円のホテルは、ちょっと何言ってるかわからない。
JCBギフトカード1万円付きはクオカードに比べてだいぶ使い勝手が良さそう。

1
1
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
1
1