Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the HTML source of a webpage in Ruby [duplicate]

Tags:

In browsers such as Firefox or Safari, with a website open, I can right click the page, and select something like: "View Page Source" or "View Source." This shows the HTML source for the page.

In Ruby, is there a function (maybe a library) that allows me to store this HTML source as a variable? Something like this:

source = view_source(http://stackoverflow.com) 

where source would be this text:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Stack Overflow</title> etc 
like image 587
Eric Avatar asked Nov 18 '10 16:11

Eric


People also ask

How do you copy the HTML of a webpage?

Copy the HTML: Press the CTRL+C shortcut to copy, or right-click on your selected text and click Copy.


1 Answers

Use Net::HTTP:

require 'net/http'  source = Net::HTTP.get('stackoverflow.com', '/index.html') 
like image 60
robbrit Avatar answered Sep 19 '22 14:09

robbrit