このページをスクレイピングしようとしています: http://www.udel.edu/dining/menus/russell.html . Hpricot ライブラリを使用して、Ruby でスクレイパーを作成しました。
問題: HTML ページがエスケープされており、エスケープせずに表示する必要がある
example: "M&M" should be "M&M"
example: "Entrée" should be "Vegetarian Entrée"
Ruby の CGI ライブラリ (あまり成功していません) と、このスタック オーバーフローの投稿で見つけた HTMLEntities gem を使用してみました。
HTMLEntities はテスト中に機能します。
require 'rubygems'
require 'htmlentities'
require 'cgi'
h = HTMLEntities.new
puts "h.decode('Entrée') = #{h.decode("Entrée")}"
blank = " "
puts "h.decode blank = #{h.decode blank}"
puts "CGI.unescapeHTML blank = |#{CGI.unescapeHTML blank}|"
puts "h.decode '<th width=86 height=59 scope=row>Vegetarian Entrée</th> ' = |#{h.decode '<th width=86 height=59 scope=row>Vegetarian Entrée</th> '}|"
正しく利回り
h.decode('Entrée') = Entrée
h.decode blank =
CGI.unescapeHTML blank = | |
h.decode '<th width=86 height=59 scope=row>Vegetarian Entrée</th> ' = |<th width=86 height=59 scope=row>Vegetarian Entrée</th> |
ただし、open-uri を使用してファイルで使用すると、正しく動作しません。
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'htmlentities'
require 'cgi'
f = open("http://www.udel.edu/dining/menus/russell.html")
htmlentity = HTMLEntities.new
while line = f.gets
puts htmlentity.decode line
end
次のようなものを誤って生成します。
<th width="60" height="59" scope="row">Vegetarian Entrée</th>
と
<th scope="row">Â </th> // note: was originally ' ' to indicate a blank
しかし、M&M は次のように正しく処理されます。
<td valign="middle" class="menulineA">M&M Brownies</td>
エスケープされた HTML を正しく処理していませんか? なぜそれが機能する場合と機能しない場合があるのか わかりません。
Ruby 1.8.7 (2009-06-12 パッチレベル 174) [i486-linux] を実行しています。
どんな助け/提案も大歓迎です。ありがとう。