Skip to content

Instantly share code, notes, and snippets.

@joohee
Last active December 18, 2015 05:29
Show Gist options
  • Save joohee/5733474 to your computer and use it in GitHub Desktop.
Save joohee/5733474 to your computer and use it in GitHub Desktop.
nokogiri encoding example (include Hangul)
# TODO : redirect link 호출 및 text data 긁어오기
require 'net/http'
require 'nokogiri'
require 'sanitize'
@newsfeed = Newsfeed.new
//...
@newsfeed.link = "http://openapi.naver.com/l?AAAD2M2w6DIAxAv6Y+GgRl8uCD29xvLFjKNFNwyC7+/TBZljTNOSdpH08KWwPdEdoKVA3ded91Cd0J2kOCLG4LNY7ea3anrVGWS131CpU10khkJRqtLdNYUU1SZEMg2wwxLiBa4Jc06+JDXPP9Re70i0KOfv73BKMz9Mnd4EBcUEe6+bCBOM9TD1xivCXej5PM3iQJpE0Sb+2IdB33VBSHVHSII06/xBjjouRKqS+DV2bW5AAAAA=="
response = Net::HTTP.get_response(URI.parse(@newsfeed.link))
response_redirect = Net::HTTP.get_response(URI.parse(response['location']))
response_redirect.body.force_encoding("euc-kr")
response_redirect.body.encode!("utf-8", :undef => :replace, :replace => "", :invalid => :replace)
html = Nokogiri::HTML(response_redirect.body)
content = html.xpath('//div[@class="article"]/div').text
stripContent = Sanitize.clean(content)
@newsfeed.content = stripContent;
@newsfeed.update_attributes(params[:newsfeed]);
puts @newsfeed.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment