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 only needs to be possible, so command is tail -f so that backend does not end even if docker-compose up
After that, enter with a command like docker exec -it backend_1 ash.
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
Create a concern that can call the chrome service of docker-compose like this, search for Jalan, and pick up the one with/ ([\ d,] +) yen /in the displayed plan name. And save it in the database.
Do this to the last page.
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 "re-search"
      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}
There is noise, but ...
 
-[Bonus shock price! Business trip support QUO card 30,000 yen plan [room without meals] large communal bath for men and women] 1 -[Bonus shock price! Business trip support QUO card with 30,000 yen plan [room without meals] large communal bath for men and women] 2 -[[Jalan Limited] ★ ☆ ★ Business trip !!! Tetsujin !!! ★ ☆ ★ [JCB gift card with 10,000 yen] Plan ♪ << with breakfast >>] 3
I don't know what a 50,000 yen hotel with a 30,000 yen quo card is saying. The JCB gift card with 10,000 yen seems to be much easier to use than the Quo card.
Recommended Posts