Closed pekorinko closed 3 years ago
begin~rescue
で囲っていることが原因begin
内のどこかでエラーが起こり、rescue
の処理に移ってしまったと思われるそのため以下にbinding.pry
を設置し、どこでエラーが起こっているのかを確認することにした
【results_controller.rb createメソッド】
def create
@url = params[:url]
url_filter = MyTools::UrlFilter.new(@url)
@url = url_filter.filter
url_validator = MyTools::UrlValidator.new(@url)
if url_validator.valid?
begin
binding.pry
place_data_scraper = MyTools::PlaceDataScraper.new(@url)
place = place_data_scraper.save_place
place_id = place.id
place_data_scraper.save_review(place_id)
@place = place_data_scraper.save_place
user_id = session[:user_id]
check_credibility = MyTools::CredibilityChecker.new(place_id)
@result = check_credibility.check(user_id)
redirect_to result_path(@result)
rescue StandardError
redirect_to root_path, alert: '口コミの取得に失敗しました'
end
else
redirect_to root_path, alert: '不正なURLです'
end
end
place = place_data_scraper.save_place
で何らかのエラーが起こっていることがわかったrescue StandardError
に移ったため
42: if url_validator.valid?
43: begin
44: binding.pry
=> 45: place_data_scraper = MyTools::PlaceDataScraper.new(@url)
46: place = place_data_scraper.save_place
47: place_id = place.id
48: place_data_scraper.save_review(place_id)
49: @place = place_data_scraper.save_place
50: user_id = session[:user_id]
51: check_credibility = MyTools::CredibilityChecker.new(place_id)
52: @result = check_credibility.check(user_id)
53: redirect_to result_path(@result)
54: rescue StandardError
55: redirect_to root_path, alert: '口コミの取得に失敗しました'
56: end
57: else
58: redirect_to root_path, alert: '不正なURLです'
59: end
60: end
42: if url_validator.valid?
43: begin
44: binding.pry
45: place_data_scraper = MyTools::PlaceDataScraper.new(@url)
=> 46: place = place_data_scraper.save_place
47: place_id = place.id
48: place_data_scraper.save_review(place_id)
49: @place = place_data_scraper.save_place
50: user_id = session[:user_id]
51: check_credibility = MyTools::CredibilityChecker.new(place_id)
52: @result = check_credibility.check(user_id)
53: redirect_to result_path(@result)
54: rescue StandardError
55: redirect_to root_path, alert: '口コミの取得に失敗しました'
56: end
57: else
58: redirect_to root_path, alert: '不正なURLです'
59: end
60: end
42: if url_validator.valid?
43: begin
44: binding.pry
45: place_data_scraper = MyTools::PlaceDataScraper.new(@url)
46: place = place_data_scraper.save_place
47: place_id = place.id
48: place_data_scraper.save_review(place_id)
49: @place = place_data_scraper.save_place
50: user_id = session[:user_id]
51: check_credibility = MyTools::CredibilityChecker.new(place_id)
52: @result = check_credibility.check(user_id)
53: redirect_to result_path(@result)
=> 54: rescue StandardError
55: redirect_to root_path, alert: '口コミの取得に失敗しました'
56: end
57: else
58: redirect_to root_path, alert: '不正なURLです'
59: end
60: end
place = place_data_scraper.save_place
の中身を詳しく見てみる【place_data_scraper save_placeメソッド】
def save_place
# 口コミが無いGoogleのURLが入力された際に発生するbugの確認をするため、以下のbinding.pryはコメントとして残す
# binding.pry
uri = URI.parse(@url)
@lrd = uri.fragment.split('=')[1].split(',')[0]
place = Place.find_by(lrd: @lrd)
@result = @scrape_process.fetch_place
if place.present?
place.update(
place_name: @result[:place_name],
address: @result[:address],
star_ave: @result[:star_ave],
)
return place
end
place_data_scraper.rb
と関連があるため内容を改めて確認@url = @driver.current_url
にて口コミURLを@urlに代入し直している(もともと@urlには検索結果URLが入っているため)
module MyTools
class SeleniumTool
attr_reader :url
def initialize(url)
@url = url
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
@driver = Selenium::WebDriver.for :chrome, options: options
@wait = Selenium::WebDriver::Wait.new(timeout: 30)
@driver.get(@url)
# モーダルが来ても検索結果が来てもページが表示されたか判別出来るなにかをする
# begin rescueを使ってタイムアウトしたということは検索画面URLだったと判断する
begin
wait.until { @driver.find_element(:class_name, 'lcorif').displayed? }
rescue StandardError
replace_url_if_needed
end
end
def replace_url_if_needed
modal =
@driver.execute_script(
'return document.getElementsByClassName("review-dialog-list").length',
)
if modal == 0
@driver.execute_script(
'document.getElementsByClassName("hqzQac")[0].getElementsByTagName("a")[0].click()',
)
sleep 5
@url = @driver.current_url
@driver.get(@url)
@wait.until { @driver.find_element(:class_name, 'lcorif').displayed? }
end
end
place_data_scraper.rb
のinitializeメソッドにおいてコードの順番にミスがあった@scrape_process = MyTools::SeleniumTool.new(@url)
をした上で@url = url
に代入しないと口コミモーダル取得後のURLをplace_data_scraper.rb
のinitializeメソッドに知らせることが出来ない
【修正前】
def initialize(url)
#TODO: URLを二重管理しているのでなんとかする https://github.com/pekorinko/review_check/issues/71
@url = url
@scrape_process = MyTools::SeleniumTool.new(@url)
end
【修正後】
def initialize(url)
#TODO: URLを二重管理しているのでなんとかする https://github.com/pekorinko/review_check/issues/71
@scrape_process = MyTools::SeleniumTool.new(url)
@url = @scrape_process.url
end
前提
selenium_tool.rb
のinitializeメソッドで検索結果URLが入力された場合は自動で「Googleの口コミ」ボタンを押す仕組みを作っている