我正在编写解析器,发现我无法解析http://ekaterinburg.irr.ru/equipment/trade/showcase/。
我有代码:
link = "http://ekaterinburg.irr.ru/equipment/trade/showcase/"
page = open(link, :allow_redirections => :all)
page = Nokogiri::HTML page
@result = page.to_html
但是如果我使用Byebug在Rails控制台中打印@result
,我只会得到这个:
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n\n
如果我尝试类似page.css('title')
的东西,也不会找到任何东西。
此代码适用于其他页面。
如果相关,我也使用
gem 'addressable'
gem 'open_uri_redirections'
[当我写Nokogiri::HTML page
时,我得到一些有关not_used_cookie
消息的信息:
(byebug) Nokogiri::HTML page
#<Nokogiri::HTML::Document:0x..fd980c3da name="document" children=[#<Nokogiri::XML::DTD:0x..fd980c20e name="html">, #<Nokogiri::XML::Element:0x..fda73e682 name="html" children=[#<Nokogiri::XML::Element:0x..fda73e4d4 name="body" children=[#<Nokogiri::XML::Element:0x..fda73e33a name="p" children=[#<Nokogiri::XML::Text:0x..fda73e10a "{\"message\":\"not_used_cookie\"}">]>]>]>]>
我进行了更多搜索,并在我解析的网站上找到此页面:
http://irr.ru/ajax/2012/common/action/passport_answer.php?message=login_fail&redirect=/&er=2
也有消息not_used_cookie
。
考虑使用Mechanize代替OpenURI。
该网站希望设置Cookie:
page = open(link)
page.read
=> "{\"message\":\"not_used_cookie\"}"
Mechanize自动存储和发送Cookie,遵循重定向,并且可以遵循链接和提交表单。