Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Access CSS values with jQuery when scraped with node.js

Access CSS values with jQuery when scraped with node.js

Problem

I am scraping a page using node.js (basically), then jQuerify the result in order to access the CSS values for some elements. However, for some reason, .css("something") always returns an empty value. E.g. .text(), however, is working fine. Any hints? Do I need to output the scraped page first before I can access the CSS?

var request = require('request');
var jsdom = require('jsdom');

var req_url = 'URL';

request({uri: req_url}, function(error, response, html){
    if(!error && response.statusCode == 200){
        var window = jsdom.jsdom(html).createWindow();
        jsdom.jQueryify(window, 'http://code.jquery.com/jquery-1.4.2.min.js', function() {
            var bg = window.$("#ID").css("color");
            console.log(bg);
        });
    }
});
Problem courtesy of: coski3003

Solution

I think that .css() will only return the inline css of a given element.

Unfortunately what you're trying to do is hard to do without an active browser.

You should look into PhantomJS-- it combines with node.js easily and acts as a headless webkit that you can inject javascript into.

Solution courtesy of: Civilian

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Access CSS values with jQuery when scraped with node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×