开发者

Ruby HTTP response html tag

I have a开发者_如何学运维 small problem in Ruby. I tried to POST some data to my web script and the response is the whole HTML page, but I need only value in div class="myclass"... How can it be done?


You will need to use an HTML parsing library like nokogiri to find the element you want within the HTML document and extract it. (You could probably work up a quick solution using regular expressions or some such, but a parsing library is much more robust for this purpose.)


This is an answer to one of the OP's comments. Droid said:

I have this html <div id="main"><div class="msgerr">I can't find it, man.</div>.... And i can't get the text even with nokogiri...

Here's how to do it:

require 'nokogiri'
html = %q{<div id="main"><div class="msgerr">I can't find it, man.</div>}

doc = Nokogiri::HTML(html)
doc.at('div.msgerr').text #=> "I can't find it, man."
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜