Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs scraping website after javascript has loaded the values

Probably a newbie question on nodejs/jsdom

I am trying to scrape a website using node.js. I am using jsdom and jquery to get the html and parse the required things. But, somehow the values i am getting are not the ones shown on the website. Basically the values are dynamically changed by javascript and i want those values. The whole reason i was using nodejs/jsdom for scraping was that js would be executed and I get the values after that event.

Is there some way to tell jsdom to wait until the javascript executes? or have i got this all wrong? I have googled a lot on this matter.

like image 784
zubinmehta Avatar asked Feb 21 '12 08:02

zubinmehta


People also ask

How do I scrape data from a website using JavaScript?

First, install Cheerio and Axios by running the following command: npm install cheerio axios . Then create a new file called crawler. js and copy/paste the following code: const axios = require('axios'); const cheerio = require('cheerio'); const getPostTitles = async () => { try { const { data } = await axios.

Is NodeJS good for scraping?

Web scraping is the process of extracting data from a website in an automated way and Node. js can be used for web scraping. Even though other languages and frameworks are more popular for web scraping, Node. js can be utilized well to do the job too.

Can we do web scraping using JavaScript?

You can do more than you think with web scraping. Once you get to know how to extract the data from websites, then you can do whatever you want with the data. The program which extracts the data from websites is called a web scraper. You are going to learn to write web scrapers in JavaScript.


1 Answers

You would be better of using something like casperjs http://casperjs.org/. It is a testing utility based on phantomjs. It is basically exactly like opening the page in a webkit browser, just without the GUI. You could write something like. I dont think it works with node, but it should be easy enough to run a casper script and pipe the output back to node.:

var casper = require('casper').create({
    loadImages: true,
    loadPlugins: true,
    verbose: true,
    //logLevel: 'info',
    clientScripts: [
        'jquery-1.7.1.min.js',
    ],
    viewportSize: {
        width: 1366,
        height: 768,
    },
    pageSettings: {
        javascriptEnabled: true,
        userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5',
    },
});

casper.start(url);

casper.thenEvaluate(function () {
    //javascript code to run in the scope of the page
});
like image 109
tapan Avatar answered Oct 24 '22 03:10

tapan