Get value from a HTTP GET response body via Nokogiri?

泄露秘密 提交于 2019-12-11 10:26:11

问题


I get this result from a HTTP page like:

<!DOCTYPE html>
<html>
  <head>
    <title>Captchaservice</title>

  </head>
  <body>
    15
  </body>
</html>

And I use this Nokogiri code:

doc = Nokogiri::HTML( response )
id = doc.xpath('//').text

But I get \n 15 \n etc.

I tried to write:

id = doc.xpath('//').text.to_i

And I get this value, but when I use this ID I get:

undefined method `empty?' for 15:Fixnum

What am I doing wrong and how do I to get this integer value?


回答1:


That's because your id is an instance of Fixnum class(as id holds the value 15 due to doc.xpath('//').text.to_i). And Fixnum class don't have #empty? method.So if you want to do empty test,then do id = doc.xpath('//').text.strip. Now your id will be having an string instance i.e. '15'. On this you can call #empty? method. when you need to use the integer value,there just do id.to_i and use it.



来源:https://stackoverflow.com/questions/19430963/get-value-from-a-http-get-response-body-via-nokogiri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!