DOMDocument in Ruby on Rails


DOMDocument in Ruby on Rails

想知道是否有可能将这个PHP脚本转换为rails

$c = file_get_contents('http://www.bunnings.com.au/products_category_plumbing-supplies_1637.aspx'); 
$dom = new DOMDocument();
$dom->loadHTML($c);    
$xpath = new DOMXPath($dom);
$div = $xpath->query('//div[@class="details"]');
echo '<table>';
foreach($div as $details)
{
    $name = $details->getElementsByTagName('h4')->item(0)->getElementsByTagName('a')->item(0)->nodeValue;
    $price = $details->getElementsByTagName('p')->item(0)->getElementsByTagName('span')->item(0)->nodeValue;
    $itemNumber = $details->getElementsByTagName('p')->item(0)->childNodes->item(2)->nodeValue;
    $html = '<tr>';
    $html .= '<td>' . htmlspecialchars($name) . '</td>';
    $html .= '<td>' . htmlspecialchars($price) . '</td>';
    $html .= '<td>' . htmlspecialchars($itemNumber) . '</td>';
    $html .= '</tr>';
    echo $html;
}
echo '</table>';

Rails是一个用于web应用程序的Ruby框架。你需要的是一个简单的Ruby脚本(当然,你可以把它集成到一个更大的Rails应用程序中)。

您可以使用nokogiri gem来解析HTML。在您的终端上:

gem install nokogiri

然后像这样创建一个新的.rb文件:

require 'open-uri'
require 'nokogiri'
url = 'http://www.bunnings.com.au/products_category_plumbing-supplies_1637.aspx'
doc = Nokogiri::HTML(open(url))
div = doc.xpath('//div[@class="details"]')
# Well, I guess you should continue now

Nokogiri的一些例子见http://www.nokogiri.org/tutorials/searching_a_xml_html_document.html