Skip to content

Instantly share code, notes, and snippets.

@hilmanski
Created September 20, 2023 22:24
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 hilmanski/ebff52f14e5662481788814095e3eb77 to your computer and use it in GitHub Desktop.
Save hilmanski/ebff52f14e5662481788814095e3eb77 to your computer and use it in GitHub Desktop.
Simple example web scraping with Ruby for beginner
require 'net/http'
require 'nokogiri'
# HTTP Client
url = 'https://www.w3schools.com'
response = Net::HTTP.get_response(URI(url))
if response.code != "200"
puts "Error: #{response.code}"
exit
end
# HTML Parser
doc = Nokogiri::HTML(response.body)
doc.css('#subtopnav a').each do |node|
print(node.content + "\n")
end
@hilmanski
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment