I'm trying to get style contents from HTML source using OpenUri and Nokogiri.
require 'open-uri'
require 'nokogiri'
require 'css_parser'
url = open('https://google.com')
html = Nokogiri::HTML(url)
css = CssParser::Parser.new
css.add_block!(html.search('style#gstyle').text)
This returns nil
, but the HTML of the Google page contains id="gstyle"
. Here is an image of the output result:
style#gstyle
?Google renders its page differently for different clients, based on the agent string, and the agent string is the only clue the server has about what kind of client is accessing the page. open-uri
by default declares itself to be "Ruby". If you are visiting with a clearly automated script, you will not get the same page as if you were visiting with a browser.
Try this:
url = open('https://google.com', "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36")