나는 종종 자신이 시나리오 이런 종류의 처리 찾기 :메서드에 오류를 반환하는 코드 블록을 전달할 수 있습니까?
require 'nokogiri'
require "open-uri"
url = "https://www.random_website.com/contains_info_I_want_to_parse"
nokodoc = Nokogiri::HTML(open(url))
# Let's say one of the following line breaks the ruby script
# because the element I'm searching doesn't contain an attribute.
a = nokodoc.search('#element-1').attribute('href').text
b = nokodoc.search('#element-2').attribute('href').text.gsub("a", "A")
c = nokodoc.search('#element-3 h1').attribute('style').text.strip
무슨 일 나는 모든 페이지에 다른 요소를 검색하는 약 30 변수를 생성 할 것이고, 내가 이상이 코드를 반복 할 것이 오 여러 페이지. 그러나 이러한 페이지 중 일부는 레이아웃이 조금씩 다를 수 있으며 해당 div 중 하나가 없습니다. 이것은 내 코드를 깨뜨릴 것이다 (예를 들어 .attribute 나 .gsub를 nil에 호출 할 수 없기 때문이다). 그러나 전 어느 선을 추측 할 수 없습니다.
url = "https://www.random_website.com/contains_info_I_want_to_parse"
nokodoc = Nokogiri::HTML(open(url))
catch_error(a, nokodoc.search('#element-1').attribute('href').text)
catch_error(b, nokodoc.search('#element-2').attribute('href').text.gsub("a", "A"))
catch_error(c, nokodoc.search('#element-3 h1').attribute('style').text.strip)
def catch_error(variable_name, code)
begin
variable_name = code
rescue
puts "Code in #{variable_name} caused an error"
end
variable_name
end
내가 각각의 새로운 방법 전에 & 퍼팅 작동하는지 알고 : 내가 좋아하는 뭔가를 할 수 있도록하고 싶습니다
begin
line #n
rescue
puts "line #n caused an error"
end
: 내 이동 - 해결책은 일반적으로 각 행을 둘러싸고 있습니다 :
하지만 내 코드에 오류가 발생하는 경우 터미널에 'puts'오류를 표시 할 수 있기를 원합니다.
가능합니까?