Created
May 19, 2020 06:25
-
-
Save junhan-z/4797ea12ed41e6771f55a1958bda2213 to your computer and use it in GitHub Desktop.
Get content from Zhihu via Ruby
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
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