Skip to content

Instantly share code, notes, and snippets.

@junhan-z
Created May 19, 2020 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junhan-z/4797ea12ed41e6771f55a1958bda2213 to your computer and use it in GitHub Desktop.
Save junhan-z/4797ea12ed41e6771f55a1958bda2213 to your computer and use it in GitHub Desktop.
Get content from Zhihu via Ruby
require 'net/http'
zhihu = 'https://zhuanlan.zhihu.com/p/139146103'
uri = URI(zhihu)
# user agent is necessary otherwise Zhihu throws 400
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36'
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|. # :use_ssl => true for the uri is https
http.request(Net::HTTP::Get.new(uri, {'User-Agent' => user_agent}))
end
put res.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment