2010-12-10 1 views
2

나는 이것에 대한 해결책을 끊임없이 찾았으며 이미지를 표시 할 때 문제를 해결했다고 생각했습니다. 그러나 엄지 손톱은 루트 요소에 저장된 것입니다. 아주 간단하게이 작동합니다Ruby, Nokogiri 및 미디어 사용 : 썸네일

rss = Nokogiri::XML(open('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml')) 
@news = rss.xpath('//item').map do |i| 
    { 
    'title'  => i.xpath('title').text, 
    'link'  => i.xpath('link').text, 
    'description' => i.xpath('description').text, 
    'thumbnail' => i.xpath('//media:thumbnail').attr('url') 
    } 
end 

그러나 미디어 편집 : 썸네일 해당 항목이 그것을 깰 것으로 보인다 참조하기 : 두 요소가 동일로

{ 
    'title'  => i.xpath('title').text, 
    'link'  => i.xpath('link').text, 
    'description' => i.xpath('description').text, 
    'thumbnail' => i.xpath('media:thumbnail').attr('url') 
} 

나는 이유를 이해하지 않습니다. 올바른 방향으로 향한 어떤 포인터라도 감사 할 것입니다.

감사합니다.

+0

입력 샘플 및 XPath 표현식 질문이 없으면이 질문은 'xpath' 질문이 아니지만 특정 언어 (루비) 클래스 메소드 질문입니다. –

답변

1

코드가 미리보기 하위 요소가없는 첫 번째 요소에서 손상됩니다. 이 시도 : 그렇지 않은 경우는 nil 존재하는 경우 또는

@news = rss.xpath('//item').map do |i| 
    thumb = i.at_xpath('media:thumbnail').attr('url') if i.at_xpath('media:thumbnail') 
    { 
    'title' => i.at_xpath('title').text, 
    'link' => i.at_xpath('link').text, 
    'description' => i.at_xpath('description').text, 
    'thumbnail' => thumb 
    } 
end 

지금 thumbnail 중 하나의 URL을 것입니다.

+0

안녕하세요! 그 점에 대해 고마워, 나는 모든 것에 대한 엄지 손톱 속성이 있다고 생각했지만 분명히 좋지 않은 해결책이었고 모든 것이 지금 완벽하게 작동하고있다 : D –

+0

미래에 누군가가 이것을 발견하면 나는 다음을 추가해야만했다 : unless! thumb.nil ? \t thumb = 'missing.jpg' \t 끝 –

+0

으로 짧게 쓸 수도 있습니다. 'thumbnail'=> thumb || 'missing.jpg' –

0

그냥 비교를 위해, 여기 노코 기리를 사용하여 노드에 액세스하는 몇 가지 다른 방법으로는 다음과 같습니다

require 'ap' 
require 'open-uri' 
require 'nokogiri' 

rss = Nokogiri::XML(open('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml')) 

# check for parsing errors... 
puts "Errors exist" if (rss.errors.any?) 

# use CSS accessors for simplicity... 
news = rss.search('item').map { |i| 

    # use CSS's namespace wildcard... 
    thumbnail = i.at('media|thumbnail') 
    begin 
    url = thumbnail['url'] 
    rescue 
    url = '' 
    end 
    { 
    'title'  => i.search('title').text, 
    'link'  => i.search('link').text, 
    'description' => i.search('description').text, 
    'thumbnail' => url 
    } 
} 

ap [*news[0 .. 1], *news[-2 .. -1]] 
# >> [ 
# >>  [0] { 
# >>    "title" => "Royal attack 'not down to radios'", 
# >>    "link" => "http://www.bbc.co.uk/go/rss/int/news/-/news/uk-11975280", 
# >>   "description" => "Police deny that a car carrying the Prince of Wales was caught up in student protests because of a breakdown in radio communications.", 
# >>   "thumbnail" => "http://news.bbcimg.co.uk/media/images/50372000/jpg/_50372715_010819577-1.jpg" 
# >>  }, 
# >>  [1] { 
# >>    "title" => "Pope Anglican offer 'dented ties'", 
# >>    "link" => "http://www.bbc.co.uk/go/rss/int/news/-/news/uk-11974543", 
# >>   "description" => "Britain's ambassador to the Vatican feared a backlash over the Pope's invitation to Anglicans to switch churches, according to leaked US cables.", 
# >>   "thumbnail" => "http://news.bbcimg.co.uk/media/images/48697000/jpg/_48697938_007958675-1.jpg" 
# >>  }, 
# >>  [2] { 
# >>    "title" => "Playing defence", 
# >>    "link" => "http://news.bbc.co.uk/go/rss/int/news/-/1/hi/programmes/from_our_own_correspondent/9275324.stm", 
# >>   "description" => "The friendly face of township where honeymoon bride was killed", 
# >>   "thumbnail" => "http://news.bbcimg.co.uk/media/images/50385000/jpg/_50385343_football_afp.jpg" 
# >>  }, 
# >>  [3] { 
# >>    "title" => "Newspaper review", 
# >>    "link" => "http://www.bbc.co.uk/go/rss/int/news/-/news/uk-11975338", 
# >>   "description" => "Papers reflect on attack on royal car", 
# >>   "thumbnail" => "http://news.bbcimg.co.uk/media/images/49254000/jpg/_49254932_efb006dc-dd09-47bd-8f2d-37752152f772.jpg" 
# >>  } 
# >> ] 

을 주목 CSS 네임 스페이스 와일드 카드의 사용. 문서의 네임 스페이스에 신경 쓰지 않고 모든 것을 처리하려는 경우 훨씬 쉽게 작업 할 수 있습니다.

+0

고맙다. 얼마나 많은 옵션이 있는지 알 수있게 해준다. nil 오류가 발생했습니다. –