Last active
October 4, 2022 19:36
-
-
Save sephraim/3db6db2d945ab384b4aa82efb3b80928 to your computer and use it in GitHub Desktop.
[Get web page source] Get the contents of a web page using Ruby's built-in open-uri library
This file contains hidden or 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 'open-uri' | |
url = 'https://www.google.com/' | |
file = URI.open(url) | |
contents = file.read | |
puts contents |
This file contains hidden or 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 'open-uri' | |
require 'htmlentities' | |
# get page source | |
url = 'https://www.google.com/' | |
source = open(url) { |f| f.read }.encode!('UTF-8', 'UTF-8', invalid: :replace) | |
# replace HTML entities | |
coder = HTMLEntities.new | |
source = coder.decode(source) | |
# split into array | |
source = source.split("\n").collect{ |line| line.strip.chomp } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment