问题
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