【发布时间】:2015-08-26 09:27:31
【问题描述】:
只是想知道是否可以将此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 中找不到类似的函数
-
您正在尝试提取 html 文档并将其放在您的页面上?当然这是可能的。有这方面的宝石。
-
@HristoGeorgiev 是的,我正在查看 httparty gem 看起来可能有用
标签: php ruby-on-rails xml domdocument data-conversion