Answer the question
In order to leave comments, you need to log in
How to convert html to text in ruby(rails)?
3rd party Api returns error as html
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>500 Server Error</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. </h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. .</p>
</body></html>
Answer the question
In order to leave comments, you need to log in
nokogiri.org
require 'nokogiri'
api_body_result = <<-HTML
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>500 Server Error</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. </h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. .</p>
</body>
</html>
HTML
doc = Nokogiri::HTML api_body_result
# source html inner text
text = doc.text
# or
# html title
title = doc.title
# html body
element = doc.at_css 'body'
body = element.text
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question