Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature find available tickets #2

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# ruby-scrapping
# rubysite scrapping process

Run `bundle install`
Run `ruby tour_site_scraper.rb`
39 changes: 39 additions & 0 deletions selenium_scraper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rubygems'
require 'selenium-webdriver'
require 'pry'

# This options are for headless execution of the browser so that it don't need to load browser
# options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless'])
# driver = Selenium::WebDriver.for(:firefox, options: options)

driver = Selenium::WebDriver.for :firefox

# driver.navigate.to "https://www.tour.ne.jp/j_air/list/?adult=1&air_type=2&arr_out=ITM&change_date_in=0&change_date_out=0&date_out=20211221&dpt_out=TYO"
driver.navigate.to "https://www.tour.ne.jp/j_air/list/?adult=1&air_type=2&arr_out=ITM&change_date_in=0&change_date_out=0&date_out=20211231&dpt_out=TYO&time_from_out=0600&time_to_out=0700&time_type_out=0"
wait = Selenium::WebDriver::Wait.new(:timeout => 10000)

#Take some time to load the page
sleep(10)

ticket_summary_button = wait.until {
elements = driver.find_element(:css, "#Act_Airline_Out")
}

sleep(10)

ticket_summary_button.click

sleep(5)

ticket_summary = driver.find_elements(:class, "airline-name")
ticket_available_lists = driver.find_elements(:class, "toggle-btn-company")

# binding.pry

available_ticket = 0

ticket_available_lists.each do |ticket_count|
available_ticket += ticket_count.text.delete("^0-9").to_i
end
puts "available ticket companies = " + ticket_summary.first.text + ", " + ticket_summary.last.text
puts "Total available ticket found is = " + available_ticket.to_s
39 changes: 39 additions & 0 deletions tour_site_scraper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rubygems'
require 'selenium-webdriver'
require 'pry'

# This options are for headless execution of the browser so that it don't need to load browser
# options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless'])
# driver = Selenium::WebDriver.for(:firefox, options: options)

driver = Selenium::WebDriver.for :firefox
#Generate the search url physically using any date, time and put here, we will make it dynamic later
driver.navigate.to "https://www.tour.ne.jp/j_air/list/?adult=1&air_type=2&arr_out=ITM&change_date_in=0&change_date_out=0&date_out=20211231&dpt_out=TYO&time_from_out=0600&time_to_out=0700&time_type_out=0"
wait = Selenium::WebDriver::Wait.new(:timeout => 10000)

#Take some time to load the page
sleep(5)

ticket_summary_button = wait.until {
elements = driver.find_element(:css, "#Act_Airline_Out")
}

ticket_summary_button.click

#Take some time after click on second tab to load ajax html content
sleep(3)

#Find available information and search companies name, this is optional
ticket_summary = driver.find_elements(:class, "airline-name")

#Find available ticke count element
ticket_available_lists = driver.find_elements(:class, "toggle-btn-company")

#Find each companies available ticket and sum to get total available tickets
total_available_ticket = 0
ticket_available_lists.each do |ticket_count|
total_available_ticket += ticket_count.text.delete("^0-9").to_i
end

puts "Available ticket companies name = " + ticket_summary.first.text + ", " + ticket_summary.last.text
puts "Total available ticket found is = " + total_available_ticket.to_s