Created
August 10, 2011 14:16
-
-
Save mathfur/1136885 to your computer and use it in GitHub Desktop.
REXML::XPath使用例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "uri" | |
require "open-uri" | |
require "rexml/document" | |
BASE_URI = "http://example.com/" | |
# urlの結果からxpathにマッチする要素全体を配列で得る | |
def get_xml_match(url,xpath) | |
open(url) do |f| | |
return REXML::XPath.match(REXML::Document.new(f.read),xpath).to_a | |
end | |
end | |
search_word = URI.encode("(検索ワードはここに)") | |
results = [] | |
get_xml_match( | |
"#{BASE_URI}/tag/#{search_word}?sort=v&rss=2.0", | |
"/rss[1]/channel[1]/item").each do |item| | |
# ヒット結果からtitleとvideo_idを取り出す | |
title = XPath.first(item,"title[1]/text()") | |
video_id = XPath.first(item,"link[1]/text()").to_s[/[^\/]+$/] | |
# video_idが指すページに書かれた情報を得る | |
results << [title.to_s,get_xml_match("#{BASE_URI}/api/getthumbinfo/#{video_id}", "//view_counter/text()").first.to_s.to_i] | |
end | |
p results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment