I’m extracting content from some websites. Currently I evaluate HTML code using Nokogiri. But the relevant content is not contained in the responded body of the HTTP GET request. This is because there is some Javascript code like $(window).load() or $(document).ready() that will send some Ajax requests and fill the original HTML code.
So I’m searching for some library that automatically executes Javascript code and Ajax requests just like a normal browser.
I'm extracting content from some websites. Currently I evaluate HTML code
using Nokogiri. But the relevant content is not contained in the responded
body of the HTTP GET request. This is because there is some Javascript code
like $(window).load() or $(document).ready() that will send some Ajax
requests and fill the original HTML code.
So I'm searching for some library that automatically executes Javascript
code and Ajax requests just like a normal browser.
Understood. Don't think I can help I am afraid. Does the site not work
with js disabled in the browser?
You obviously can’t do this with Ruby alone, as there is no headless web browser written entirely in Ruby (that’s just nonsense)
If you can get phantomjs working on your production site, that’s probably the way to go. Look deep into the internals of Capybara to understand how it drives phantomjs. With phantomjs, you basically have a headless web browser and you can use Capybara’s DSL to access parts of the page, including evaluating scripts and parsing the DOM.
Just keep in mind phantomjs is an actual executable so it needs to be compiled and built for your production environment explicitly, which might be a little tricky depending on where your site is.
But a little birdie told me a few months ago that the phantomjs team has decided that once Chrome has a headless mode, which I believe is forthcoming, they plan to abandon phantomjs in favor of Chrome’s headless mode. Not sure if that’s really true or when that will happen.