获取存储在URL中的数据

时间:2013-10-07 06:23:44

标签: ruby-on-rails

此处新增内容,想知道如何从特定URL获取数据并将相应的数据存储在数据库中,然后使用Rails访问它。

但是,我能够从URL获取数据并以XML格式接收数据并能够显示它,但这是手动完成的,我想知道的是如何从URL获取数据,因为它是Hash形式,包含很多属性。

需要将这些属性存储在数据库中并直接从URL中检索值。

2 个答案:

答案 0 :(得分:1)

for that use Nokogiri gem for more information you can read from http://nokogiri.org/tutorials/parsing_an_html_xml_document.html

I also gives you following commands of nokogiri... please avoide # sign

doc = Nokogiri::HTML(open(your site url))
# get all specific selector's all matching elements
# doc.css("div")

# get specific selector's first matching element
# doc.at_css("div")

# get matching element by id name
# doc.at_css("input#id name")
# eg: doc.at_css("input#ResultsCount")

# get matching element by class name
# doc.at_css("div.class name")
# eg: doc.at_css("div.results")


# File.open("#{Rails.root}/public/aa.txt","w+").write(doc.css("div#search-result-listings"))

# get fields data eg. take a value of input field whose id ResultsCount
# <input type="hidden" name="ResultsCount" id="ResultsCount" value="12321" />
# doc.at_css("input#ResultsCount")["value"]

# get all results
# search_results=doc.at_css("div#search-result-listings").css("div.result.clearfix")


#find by tag ("<ul>") and find their elements and children
dc=doc.at_css("div#search-result-listings")
#find all elements of ul such as li with their childs
dc.at_css("ul").elements
#if only childs of elements
dc.at_css("ul").elements.children
#if you want to print that child value then use "text" property
dc.at_css("ul").elements.children[0].text
#if you want all child data then use
dc.at_css("ul").elements.children.text
or
dc.at_css("ul").elements.text
or
dc.at_css("ul").text

答案 1 :(得分:0)

您可以使用Mechanize获取页面,使用Nokogiri解析内容,并使用Nokogiri::XML::Builder(或Builder)从接收的数据构建XML,或将其存储在数据库中。

相关问题