Last active
December 18, 2015 05:29
-
-
Save joohee/5733474 to your computer and use it in GitHub Desktop.
nokogiri encoding example (include Hangul)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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